quran-go

Read Qur'an right in the terminal.
git clone http://git.hanabi.in/repos/quran-go.git
Log | Files | Refs | README | LICENSE

parse-input.go (1202B)


      1 package utils
      2 
      3 import (
      4 	"fmt"
      5 	"regexp"
      6 	"strconv"
      7 
      8 	t "git.hanabi.in/gitbox/quran-go/src/types"
      9 )
     10 
     11 // Takes "1" or "1:2" or "1:2-5" and returns chapter, verse1, verse2 and error, if any.
     12 func ParseInput(input string) (chapter t.Chap, verse1, verse2 t.Verse, err error) {
     13 
     14 	// Group 1, 3, 5 = Chapter:Verse1-Verse2
     15 	re := regexp.MustCompile(`^(\d+)(:(\d+)(-(\d+))?)?$`)
     16 
     17 	isValid := re.Match([]byte(input))
     18 	if !isValid {
     19 		err = fmt.Errorf("Expected in the form `quran [-lang=code] chapter[:verse1[-verse2]]'\n")
     20 		return
     21 	}
     22 
     23 	res := re.FindAllStringSubmatch(input, -1)
     24 
     25 	ch, err := strconv.ParseUint(res[0][1], 10, 64)
     26 	if err != nil {
     27 		return
     28 	}
     29 
     30 	chapter = t.Chap(ch)
     31 
     32 	if res[0][3] == res[0][5] && len(res[0][3]) > 0 {
     33 		err = fmt.Errorf("Verse 2 should not be same as Verse 1.\n")
     34 		return
     35 	}
     36 
     37 	if res[0][3] == "" {
     38 		res[0][3] = "0"
     39 	}
     40 
     41 	ve1, err := strconv.ParseUint(res[0][3], 10, 64)
     42 	if err != nil {
     43 		return
     44 	}
     45 
     46 	verse1 = t.Verse(ve1)
     47 
     48 	if res[0][5] == "" {
     49 		res[0][5] = res[0][3]
     50 	}
     51 
     52 	ve2, err := strconv.ParseUint(res[0][5], 10, 64)
     53 	if err != nil {
     54 		return
     55 	}
     56 
     57 	verse2 = t.Verse(ve2)
     58 
     59 	if verse2 < verse1 {
     60 		err = fmt.Errorf("Verse 2 must be bigger than Verse 1.\n")
     61 	}
     62 
     63 	return
     64 
     65 }