Skip to content

Commit 0faef40

Browse files
committed
feat(string): new builtin string:utf8len
1 parent 7eb3f2a commit 0faef40

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

String.ark

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@
6868
# @author https://github.com/SuperFola
6969
(let removeAt (fun (_str _index) (builtin__string:removeAt _str _index)))
7070

71+
# @brief Compute the length of an UTF8 string by counting the number of codepoints
72+
# @param char a String
73+
# =begin
74+
# (string:utf8len "h") # 1
75+
# (string:utf8len "🏳️‍⚧️") # 5
76+
# =end
77+
# @author https://github.com/SuperFola
78+
(let utf8len (fun (_str) (builtin__string:utf8len _str)))
79+
7180
# @brief Get the ordinal of a given character
7281
# @param char a String with a single UTF8 character
7382
# =begin

tests/string-tests.ark

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,16 @@
3838
(test:eq (string:emptyOrWhitespace? "a ") false)
3939
(test:eq (string:emptyOrWhitespace? " a ") false) })
4040

41+
(test:case "utf8len" {
42+
(test:eq (builtin__string:utf8len "hello") (string:utf8len "hello"))
43+
(test:eq (string:utf8len "h") 1)
44+
(test:eq (string:utf8len "hello") 5)
45+
(test:eq (string:utf8len "hello\t") 6)
46+
(test:eq (string:utf8len "🏳️‍⚧️") 5) })
47+
4148
(test:case "ord, chr" {
4249
(test:eq (builtin__string:ord "a") (string:ord "a"))
50+
(test:eq (string:ord "ø") 248)
4351
(test:eq (builtin__string:chr 65) (string:chr 65)) })
4452

4553
(test:case "count" {

0 commit comments

Comments
 (0)