Skip to content

Commit

Permalink
Add String.utf16_strlen
Browse files Browse the repository at this point in the history
Signed-off-by: Jiří Janoušek <[email protected]>
  • Loading branch information
jiri-janousek committed May 27, 2017
1 parent 027bc69 commit f1f0f7a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/glib/String.vala
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:
Expand Down Expand Up @@ -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

0 comments on commit f1f0f7a

Please sign in to comment.