doujiang3997 2019-08-14 06:50
浏览 105
已采纳

检查符文是否在基本多语言平面中的正确方法是什么?

I want to check, whether a given rune is in a basic multilingual plane or not.

That is, what to put in this function - https://play.golang.org/p/3szTn8pP7xe

package main

import (
"fmt"
)

func isBMP(r rune) bool {
// ???
return false
}

func main() {
fmt.Println(isBMP(rune('պ'))) // expect true
fmt.Println(isBMP(rune('

  • 写回答

2条回答 默认 最新

  • dongyan1841 2019-08-14 07:08
    关注

    Basic Multilingual Plane have the following code point ranges allocated:

    0000–​0FFF    8000–​8FFF
    1000–​1FFF    9000–​9FFF
    2000–​2FFF    A000–​AFFF
    3000–​3FFF    B000–​BFFF
    4000–​4FFF    C000–​CFFF
    5000–​5FFF    D000–​DFFF
    6000–​6FFF    E000–​EFFF
    7000–​7FFF    F000–​FFFF
    

    So to tell if a rune falls in the basic multilingual plane, just check if it falls inside any of these ranges. Since these ranges cover all values between 0 and 0xffff (both inclusive), just check it like this:

    func isBMP(r rune) bool {
        return r >= 0 && r <= 0xffff
    }
    

    Note that since rune is alias for int32, it may have negative values, so also checking if it's not negative is important.

    This will output your expected result. Try it on the Go Playground.

    Note #2: iterating over the runes of a string which contains invalid UTF-8 bytes, you will get the Unicode replacement character for the invalid bytes, which is 0xfffd. If you want to exclude those from your test, you could modify it like:

    func isBMP(r rune) bool {
        return r >= 0 && r <= 0xffff && r != 0xfffd
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?