This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -1,12 +1,32 @@
set inString to "Hello World" as Unicode text
set inString to "Hello é̦世界"
set byteCount to 0
set idList to id of inString
repeat with incr in idList
set byteCount to byteCount + 2
if incr as integer > 65535 then
set byteCount to byteCount + 2
end if
repeat with c in inString
set t to id of c
if ((count of t) > 0) then
repeat with i in t
set byteCount to byteCount + doit(i)
end repeat
else
set byteCount to byteCount + doit(t)
end if
end repeat
byteCount
on doit(cid)
set n to (cid as integer)
if n > 67108863 then -- 0x3FFFFFF
return 6
else if n > 2097151 then -- 0x1FFFFF
return 5
else if n > 65535 then -- 0xFFFF
return 4
else if n > 2047 then -- 0x07FF
return 3
else if n > 127 then -- 0x7F
return 2
else
return 1
end if
end doit

View file

@ -0,0 +1 @@
? LEN("HELLO, WORLD!")

View file

@ -0,0 +1,2 @@
(length "hello")
=> 5

View file

@ -0,0 +1,2 @@
(string-bytes "\u1D518\u1D52B\u1D526")
=> 12

View file

@ -0,0 +1,7 @@
(let ((str (apply 'string
(mapcar (lambda (c) (decode-char 'ucs c))
'(#x1112 #x1161 #x11ab #x1100 #x1173 #x11af)))))
(list (length str)
(string-bytes str)
(string-width str)))
=> (6 18 4) ;; in emacs 23 up

View file

@ -0,0 +1,2 @@
print strlen("hello")
=> 5

View file

@ -1,2 +1,4 @@
String s = "Hello, world!";
int byteCount = s.length() * 2;
int byteCountUTF16 = s.getBytes("UTF-16").length; // Incorrect it yield 16 that is with the BOM
int byteCountUTF16 = s.getBytes("UTF-16LE").length; // Correct it yield 14
int byteCountUTF8 = s.getBytes("UTF-8").length;

View file

@ -1,3 +1,2 @@
String s = "Hello, world!";
int byteCountUTF16 = s.getBytes("UTF-16").length;
int byteCountUTF8 = s.getBytes("UTF-8").length;
int not_really_the_length = s.length(); // XXX: does not (always) count Unicode characters (code points)!

View file

@ -1,2 +1,3 @@
String s = "Hello, world!";
int not_really_the_length = s.length(); // XXX: does not (always) count Unicode characters (code points)!
String str = "\uD834\uDD2A"; //U+1D12A
int not_really__the_length = str.length(); // value is 2, which is not the length in characters
int actual_length = str.codePointCount(0, str.length()); // value is 1, which is the length in characters

View file

@ -1,3 +1,19 @@
String str = "\uD834\uDD2A"; //U+1D12A
int not_really__the_length = str.length(); // value is 2, which is not the length in characters
int actual_length = str.codePointCount(0, str.length()); // value is 1, which is the length in characters
import java.text.BreakIterator;
public class Grapheme {
public static void main(String[] args) {
printLength("møøse");
printLength("𝔘𝔫𝔦𝔠𝔬𝔡𝔢");
printLength("J̲o̲s̲é̲");
}
public static void printLength(String s) {
BreakIterator it = BreakIterator.getCharacterInstance();
it.setText(s);
int count = 0;
while (it.next() != BreakIterator.DONE) {
count++;
}
System.out.println("Grapheme length: " + count+ " " + s);
}
}

View file

@ -0,0 +1,4 @@
var s: string = "Hello, world! ☺"
echo '"',s, '"'," has byte length: ", len(s)
# -> "Hello, world! ☺" has unicode char length: 17

View file

@ -0,0 +1,6 @@
import unicode
var s: string = "Hello, world! ☺"
echo '"',s, '"'," has unicode char length: ", runeLen(s)
# -> "Hello, world! ☺" has unicode char length: 15

View file

@ -0,0 +1,11 @@
object StringLength extends App {
val s1 = "møøse"
val s3 = List("\uD835\uDD18", "\uD835\uDD2B", "\uD835\uDD26",
"\uD835\uDD20", "\uD835\uDD2C", "\uD835\uDD21", "\uD835\uDD22").mkString
val s4 = "J\u0332o\u0332s\u0332e\u0301\u0332"
List(s1, s3, s4).foreach(s => println(
s"The string: $s, characterlength= ${s.length} UTF8bytes= ${
s.getBytes("UTF-8").size
} UTF16bytes= ${s.getBytes("UTF-16LE").size}"))
}