What is the easiest way to determine if a character is in Unicode range, in Rust?

StackOverflow https://stackoverflow.com/questions/18331546

  •  25-06-2022
  •  | 
  •  

문제

I'm looking for easiest way to determine if a character in Rust is between two Unicode values.

For example, I want to know if a character s is between [#x1-#x8] or [#x10FFFE-#x10FFFF]. Is there a function that does this already?

도움이 되었습니까?

해결책

The simplest way for me to match a character was this

fn match_char(data: &char) -> bool {
    match *data {
        '\x01'...'\x08' |
        '\u{10FFFE}'...'\u{10FFFF}' => true,
        _ => false,
    }
}

Pattern matching a character was the easiest route for me, compared to a bunch of if statements. It might not be the most performant solution, but it served me very well.

다른 팁

The simplest way, assuming that they are not Unicode categories (in which case you should be using std::unicode) is to use the regular comparison operators:

(s >= '\x01' && s <= '\x08') || s == '\U0010FFFE' || s == '\U0010FFFF'

(In case you weren't aware of the literal forms of these things, one gets 8-bit hexadecimal literals \xXX, 16-bit hexadecimal literals \uXXXX, and 32-bit hexadecimal literals \UXXXXXXXX. Matter of fact, casts would work fine too, e.g. 0x10FFFE as char, and would be just as efficient; just less easily readable.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top