doudai3012 2013-11-24 15:01
浏览 58
已采纳

unicode.RangeTable如何工作?

I'd like some help on understanding the unicode package's RangeTable.

Using this (supposedly helping) function:

func printChars(ranges []unicode.Range16) {
  for _, r := range ranges {

    if r.Hi >= 0x80 { // show only ascii
      break
    }
    fmt.Println("
Lo:", r.Lo, "Hi:", r.Hi, "Stride:", r.Stride)

    for c := r.Lo; c <= r.Hi; c++ {
      fmt.Print(string(c) + " ")
    }
  }
  fmt.Println()
}

For digits, I can do printChars(unicode.Digit.R16), and the sequence of digits make sense to me.

 // Lo: 48 Hi: 57 Stride: 1
 // 0 1 2 3 4 5 6 7 8 9

However, to get punctuation printChars(unicode.Punct.R16) results in

 // Lo: 33 Hi: 35 Stride: 1
 // ! " #
 // Lo: 37 Hi: 42 Stride: 1
 // % & ' ( ) *
 // Lo: 44 Hi: 47 Stride: 1
 //  , - . /
 // Lo: 58 Hi: 59 Stride: 1
 // : ;
 // Lo: 63 Hi: 64 Stride: 1
 // ? @
 // Lo: 91 Hi: 93 Stride: 1
 // [ \ ]
 // Lo: 95 Hi: 123 Stride: 28
 // _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z {

I'm surprised that the lower case letters are included too. Also, what does "Stride" mean? It's 1 for all but the last, but the hi-lo difference varies.

As another example, printChars(unicode.Pe.R16). I thought this should give only the end punctuation:

  • ) right parenthesis (U+0029, Pe)
  • ] right square bracket (U+005D, Pe)
  • } right curly bracket (U+007D, Pe)

But instead my function prints

 // Lo: 41 Hi: 93 Stride: 52
 // ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ]

Presumably I'm completely misunderstanding the way this is supposed to work.

How might I correctly get a list of characters in a given category, for example, Punctuation End (Pe) as above?

  • 写回答

2条回答 默认 最新

  • dsa456369 2013-11-24 16:12
    关注

    Stride is the step with which you have to iterate over the range. Let's heighten the boundary of 0x80 a bit and make the loop to iterate using Stride:

    package main
    
    import (
        "fmt"
        "unicode"
    )
    
    func printChars(ranges []unicode.Range16) {
      for _, r := range ranges {
    
        if r.Hi >= 0x100 {
          break
        }
        fmt.Println("
    Lo:", r.Lo, "Hi:", r.Hi, "Stride:", r.Stride)
    
        for c := r.Lo; c <= r.Hi; c+=r.Stride {
          fmt.Print(string(c) + " ")
        }
      }
      fmt.Println()
    }
    
    func main() {
        printChars(unicode.Punct.R16)
    }
    

    And here is the output:

    % go run main.go
    
    Lo: 33 Hi: 35 Stride: 1
    ! " # 
    Lo: 37 Hi: 42 Stride: 1
    % & ' ( ) * 
    Lo: 44 Hi: 47 Stride: 1
    , - . / 
    Lo: 58 Hi: 59 Stride: 1
    : ; 
    Lo: 63 Hi: 64 Stride: 1
    ? @ 
    Lo: 91 Hi: 93 Stride: 1
    [ \ ] 
    Lo: 95 Hi: 123 Stride: 28
    _ { 
    Lo: 125 Hi: 161 Stride: 36
    } ¡ 
    Lo: 167 Hi: 171 Stride: 4
    § « 
    Lo: 182 Hi: 183 Stride: 1
    ¶ · 
    Lo: 187 Hi: 191 Stride: 4
    » ¿ 
    

    Looks pretty much correct to me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持