-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jiří Janoušek <[email protected]>
- Loading branch information
1 parent
027bc69
commit f1f0f7a
Showing
1 changed file
with
21 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2016 Jiří Janoušek <[email protected]> | ||
* Copyright 2017 Jiří Janoušek <[email protected]> | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
|
@@ -145,4 +145,24 @@ public string? unmask(uint8[] data) | |
return (string) result; | ||
} | ||
|
||
/** | ||
* Return the number of UTF-16 code points to store given string. | ||
* | ||
* It corresponds to JavaScript's String.length. | ||
* | ||
* @param str a string | ||
* @return a number of UTF-16 code points | ||
*/ | ||
public int utf16_strlen(string? str) | ||
{ | ||
if (is_empty(str)) | ||
return 0; | ||
int len = 0; | ||
unichar c = 0; | ||
int i = 0; | ||
while (str.get_next_char(ref i, out c)) | ||
len += (uint) c <= 0xFFFF ? 1 : 2; | ||
return len; | ||
} | ||
|
||
} // namespace Diorite.String |