Browse Source

Merge pull request #4402 from Lperlind/utf16_rune_count

core/unicode/utf16: add rune_count proc
gingerBill 10 months ago
parent
commit
f047f804f6
1 changed files with 13 additions and 1 deletions
  1. 13 1
      core/unicode/utf16/utf16.odin

+ 13 - 1
core/unicode/utf16/utf16.odin

@@ -106,6 +106,18 @@ decode :: proc(d: []rune, s: []u16) -> (n: int) {
 	return
 }
 
+rune_count :: proc(s: []u16) -> (n: int) {
+	for i := 0; i < len(s); i += 1 {
+		c := s[i]
+		if _surr1 <= c && c < _surr2 && i+1 < len(s) && 
+			_surr2 <= s[i+1] && s[i+1] < _surr3 {
+			i += 1
+		}
+		n += 1
+	}
+	return
+}
+
 
 decode_to_utf8 :: proc(d: []byte, s: []u16) -> (n: int) {
 	for i := 0; i < len(s); i += 1 {
@@ -127,4 +139,4 @@ decode_to_utf8 :: proc(d: []byte, s: []u16) -> (n: int) {
 		n += copy(d[n:], b[:w])
 	}
 	return
-}
+}