forked from Mirror/wren
Rationalize string lengths.
The .count getter on string returns the number of code points. That's O(n), but it's consistent with the rest of the main string API. If you want the number of bytes, it's "string".bytes.count. Updated the docs. Fixes 68. Woo!
This commit is contained in:
@ -6,3 +6,13 @@ IO.print("\0".count) // expect: 1
|
||||
IO.print("a\0b".count) // expect: 3
|
||||
IO.print("\0c".count) // expect: 2
|
||||
IO.print(("a\0b" + "\0c").count) // expect: 5
|
||||
|
||||
// Treats a UTF-8 sequence as a single item.
|
||||
//
|
||||
// Bytes: 11111
|
||||
// 012345678901234
|
||||
// Chars: sø mé ஃ thî ng
|
||||
IO.print("søméஃthîng".count) // expect: 10
|
||||
|
||||
// Counts invalid UTF-8 one byte at a time.
|
||||
IO.print("\xefok\xf7".count) // expect: 4
|
||||
|
||||
18
test/core/string_code_point_sequence/count.wren
Normal file
18
test/core/string_code_point_sequence/count.wren
Normal file
@ -0,0 +1,18 @@
|
||||
IO.print("".codePoints.count) // expect: 0
|
||||
IO.print("a string".codePoints.count) // expect: 8
|
||||
|
||||
// 8-bit clean.
|
||||
IO.print("\0".codePoints.count) // expect: 1
|
||||
IO.print("a\0b".codePoints.count) // expect: 3
|
||||
IO.print("\0c".codePoints.count) // expect: 2
|
||||
IO.print(("a\0b" + "\0c").codePoints.count) // expect: 5
|
||||
|
||||
// Treats a UTF-8 sequence as a single item.
|
||||
//
|
||||
// Bytes: 11111
|
||||
// 012345678901234
|
||||
// Chars: sø mé ஃ thî ng
|
||||
IO.print("søméஃthîng".codePoints.count) // expect: 10
|
||||
|
||||
// Counts invalid UTF-8 one byte at a time.
|
||||
IO.print("\xefok\xf7".codePoints.count) // expect: 4
|
||||
Reference in New Issue
Block a user