github.com/couchbaselabs/nex@v0.0.0-20230419191105-421cb5932838/test/u.nex (about)

     1  /[零一二三四五六七八九十百千]+/ { fmt.Print(zhToInt(txt())) }
     2  /[٠-٩]/ {
     3    // The above character class might show up right-to-left in a browser.
     4    // The equivalent of 0 should be on the left, and the equivalent of 9 should
     5    // be on the right.
     6    //
     7    // The Eastern Arabic numerals are ٠١٢٣٤٥٦٧٨٩.
     8    fmt.Print([]rune(txt())[0] - rune('٠'))
     9  }
    10  /./ { fmt.Print(txt()) }
    11  //
    12  package main
    13  import ("fmt";"os")
    14  func zhToInt(s string) int {
    15    n := 0
    16    prev := 0
    17    f := func(m int) {
    18      if 0 == prev { prev = 1 }
    19      n += m * prev
    20      prev = 0
    21    }
    22    for _, c := range s {
    23      for m, v := range []rune("一二三四五六七八九") {
    24        if v == c {
    25  	prev = m+1
    26  	goto continue2
    27        }
    28      }
    29      switch c {
    30      case '零':
    31      case '十': f(10)
    32      case '百': f(100)
    33      case '千': f(1000)
    34      }
    35  continue2:
    36    }
    37    n += prev
    38    return n
    39  }
    40  func main() {
    41    lex := NewLexer(os.Stdin)
    42    txt := func() string { return lex.Text() }
    43    NN_FUN(lex)
    44  }