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 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败