all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
5
Task/String-length/0DESCRIPTION
Normal file
5
Task/String-length/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
In this task, the goal is to find the <em>character</em> and <em>byte</em> length of a string. This means encodings like [[UTF-8]] need to be handled properly, as there is not necessarily a one-to-one relationship between bytes and characters. By ''character'', we mean an individual Unicode ''code point'', not a user-visible ''grapheme'' containing combining characters. For example, the character length of "møøse" is 5 but the byte length is 7 in UTF-8 and 10 in UTF-16.
|
||||
|
||||
Non-BMP code points (those between 0x10000 and 0x10FFFF) must also be handled correctly: answers should produce actual character counts in code points, not in code unit counts. Therefore a string like "𝔘𝔫𝔦𝔠𝔬𝔡𝔢" (consisting of the 7 Unicode characters U+1D518 U+1D52B U+1D526 U+1D520 U+1D52C U+1D521 U+1D522) is 7 characters long, '''not''' 14 UTF-16 code units; and it is 28 bytes long whether encoded in UTF-8 or in UTF-16.
|
||||
|
||||
Please mark your examples with <nowiki>===Character Length=== or ===Byte Length===</nowiki>. If your language is capable of providing the string length in graphemes, mark those examples with <nowiki>===Grapheme Length===</nowiki>. For example, the string "J̲o̲s̲é̲" ("J\x{332}o\x{332}s\x{332}e\x{301}\x{332}") has 4 user-visible graphemes, 9 characters (code points), and 14 bytes when encoded in UTF-8.
|
||||
2
Task/String-length/1META.yaml
Normal file
2
Task/String-length/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Basic language learning
|
||||
1
Task/String-length/4D/string-length.4d
Normal file
1
Task/String-length/4D/string-length.4d
Normal file
|
|
@ -0,0 +1 @@
|
|||
$length:=Length("Hello, world!")
|
||||
7
Task/String-length/ALGOL-68/string-length-1.alg
Normal file
7
Task/String-length/ALGOL-68/string-length-1.alg
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
BITS bits := bits pack((TRUE, TRUE, FALSE, FALSE)); # packed array of BOOL #
|
||||
BYTES bytes := bytes pack("Hello, world"); # packed array of CHAR #
|
||||
print((
|
||||
"BITS and BYTES are fixed width:", new line,
|
||||
"bits width:", bits width, ", max bits: ", max bits, ", bits:", bits, new line,
|
||||
"bytes width: ",bytes width, ", UPB:",UPB STRING(bytes), ", string:", STRING(bytes),"!", new line
|
||||
))
|
||||
7
Task/String-length/ALGOL-68/string-length-2.alg
Normal file
7
Task/String-length/ALGOL-68/string-length-2.alg
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
STRING str := "hello, world";
|
||||
INT length := UPB str;
|
||||
printf(($"Length of """g""" is "g(3)l$,str,length));
|
||||
|
||||
printf(($l"STRINGS can start at -1, in which case LWB must be used:"l$));
|
||||
STRING s := "abcd"[@-1];
|
||||
print(("s:",s, ", LWB:", LWB s, ", UPB:",UPB s, ", LEN:",UPB s - LWB s + 1))
|
||||
4
Task/String-length/AWK/string-length-1.awk
Normal file
4
Task/String-length/AWK/string-length-1.awk
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
w=length("Hello, world!") # static string example
|
||||
x=length("Hello," s " world!") # dynamic string example
|
||||
y=length($1) # input field example
|
||||
z=length(s) # variable name example
|
||||
2
Task/String-length/AWK/string-length-2.awk
Normal file
2
Task/String-length/AWK/string-length-2.awk
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#!/usr/bin/awk -f
|
||||
{print"The length of this line is "length($0)}
|
||||
1
Task/String-length/ActionScript/string-length.as
Normal file
1
Task/String-length/ActionScript/string-length.as
Normal file
|
|
@ -0,0 +1 @@
|
|||
myStrVar.length()
|
||||
2
Task/String-length/Ada/string-length-1.ada
Normal file
2
Task/String-length/Ada/string-length-1.ada
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Str : String := "Hello World";
|
||||
Length : constant Natural := Str'Size / 8;
|
||||
6
Task/String-length/Ada/string-length-2.ada
Normal file
6
Task/String-length/Ada/string-length-2.ada
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Latin_1_Str : String := "Hello World";
|
||||
UCS_16_Str : Wide_String := "Hello World";
|
||||
Unicode_Str : Wide_Wide_String := "Hello World";
|
||||
Latin_1_Length : constant Natural := Latin_1_Str'Length;
|
||||
UCS_16_Length : constant Natural := UCS_16_Str'Length;
|
||||
Unicode_Length : constant Natural := Unicode_Str'Length;
|
||||
|
|
@ -0,0 +1 @@
|
|||
count of "Hello World"
|
||||
12
Task/String-length/AppleScript/string-length-2.applescript
Normal file
12
Task/String-length/AppleScript/string-length-2.applescript
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
set inString to "Hello World" as Unicode text
|
||||
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
|
||||
end repeat
|
||||
|
||||
byteCount
|
||||
|
|
@ -0,0 +1 @@
|
|||
count of "Hello World"
|
||||
|
|
@ -0,0 +1 @@
|
|||
count "Hello World"
|
||||
1
Task/String-length/AutoHotkey/string-length-1.ahk
Normal file
1
Task/String-length/AutoHotkey/string-length-1.ahk
Normal file
|
|
@ -0,0 +1 @@
|
|||
Msgbox % StrLen("Hello World")
|
||||
3
Task/String-length/AutoHotkey/string-length-2.ahk
Normal file
3
Task/String-length/AutoHotkey/string-length-2.ahk
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
String := "Hello World"
|
||||
StringLen, Length, String
|
||||
Msgbox % Length
|
||||
2
Task/String-length/BASIC/string-length-1.basic
Normal file
2
Task/String-length/BASIC/string-length-1.basic
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
INPUT a$
|
||||
PRINT LEN(a$)
|
||||
2
Task/String-length/BASIC/string-length-2.basic
Normal file
2
Task/String-length/BASIC/string-length-2.basic
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
10 INPUT a$
|
||||
20 PRINT LEN(a$)
|
||||
2
Task/String-length/BBC-BASIC/string-length-1.bbc
Normal file
2
Task/String-length/BBC-BASIC/string-length-1.bbc
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
INPUT text$
|
||||
PRINT LEN(text$)
|
||||
12
Task/String-length/BBC-BASIC/string-length-2.bbc
Normal file
12
Task/String-length/BBC-BASIC/string-length-2.bbc
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
CP_ACP = 0
|
||||
CP_UTF8 = &FDE9
|
||||
|
||||
textA$ = "møøse"
|
||||
textW$ = " "
|
||||
textU$ = " "
|
||||
|
||||
SYS "MultiByteToWideChar", CP_ACP, 0, textA$, -1, !^textW$, LEN(textW$)/2 TO nW%
|
||||
SYS "WideCharToMultiByte", CP_UTF8, 0, textW$, -1, !^textU$, LEN(textU$), 0, 0
|
||||
PRINT "Length in bytes (ANSI encoding) = " ; LEN(textA$)
|
||||
PRINT "Length in bytes (UTF-16 encoding) = " ; 2*(nW%-1)
|
||||
PRINT "Length in bytes (UTF-8 encoding) = " ; LEN($$!^textU$)
|
||||
17
Task/String-length/Batch-File/string-length.bat
Normal file
17
Task/String-length/Batch-File/string-length.bat
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
call :length %1 res
|
||||
echo length of %1 is %res%
|
||||
goto :eof
|
||||
|
||||
:length
|
||||
set str=%~1
|
||||
set cnt=0
|
||||
:loop
|
||||
if "%str%" equ "" (
|
||||
set %2=%cnt%
|
||||
goto :eof
|
||||
)
|
||||
set str=!str:~1!
|
||||
set /a cnt = cnt + 1
|
||||
goto loop
|
||||
7
Task/String-length/Bracmat/string-length-1.bracmat
Normal file
7
Task/String-length/Bracmat/string-length-1.bracmat
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(ByteLength=
|
||||
length
|
||||
. @(!arg:? [?length)
|
||||
& !length
|
||||
);
|
||||
|
||||
out$ByteLength$𝔘𝔫𝔦𝔠𝔬𝔡𝔢
|
||||
16
Task/String-length/Bracmat/string-length-2.bracmat
Normal file
16
Task/String-length/Bracmat/string-length-2.bracmat
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
(CharacterLength=
|
||||
length c
|
||||
. 0:?length
|
||||
& @( !arg
|
||||
: ?
|
||||
( %?c
|
||||
& utf$!c:?k
|
||||
& 1+!length:?length
|
||||
& ~
|
||||
)
|
||||
?
|
||||
)
|
||||
| !length
|
||||
);
|
||||
|
||||
out$CharacterLength$𝔘𝔫𝔦𝔠𝔬𝔡𝔢
|
||||
14
Task/String-length/Bracmat/string-length-3.bracmat
Normal file
14
Task/String-length/Bracmat/string-length-3.bracmat
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(CharacterLength=
|
||||
length c p
|
||||
. 0:?length:?p
|
||||
& @( !arg
|
||||
: ?
|
||||
( [!p %?c
|
||||
& utf$!c:?k
|
||||
& 1+!length:?length
|
||||
)
|
||||
([?p&~)
|
||||
?
|
||||
)
|
||||
| !length
|
||||
);
|
||||
11
Task/String-length/C++/string-length-1.cpp
Normal file
11
Task/String-length/C++/string-length-1.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include <string> // (not <string.h>!)
|
||||
using std::string;
|
||||
|
||||
int main()
|
||||
{
|
||||
string s = "Hello, world!";
|
||||
string::size_type length = s.length(); // option 1: In Characters/Bytes
|
||||
string::size_type size = s.size(); // option 2: In Characters/Bytes
|
||||
// In bytes same as above since sizeof(char) == 1
|
||||
string::size_type bytes = s.length() * sizeof(string::value_type);
|
||||
}
|
||||
8
Task/String-length/C++/string-length-2.cpp
Normal file
8
Task/String-length/C++/string-length-2.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <string>
|
||||
using std::wstring;
|
||||
|
||||
int main()
|
||||
{
|
||||
wstring s = L"\u304A\u306F\u3088\u3046";
|
||||
wstring::size_type length = s.length() * sizeof(wstring::value_type); // in bytes
|
||||
}
|
||||
8
Task/String-length/C++/string-length-3.cpp
Normal file
8
Task/String-length/C++/string-length-3.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <string>
|
||||
using std::wstring;
|
||||
|
||||
int main()
|
||||
{
|
||||
wstring s = L"\u304A\u306F\u3088\u3046";
|
||||
wstring::size_type length = s.length();
|
||||
}
|
||||
9
Task/String-length/C++/string-length-4.cpp
Normal file
9
Task/String-length/C++/string-length-4.cpp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#include <iostream>
|
||||
#include <codecvt>
|
||||
int main()
|
||||
{
|
||||
std::string utf8 = "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b"; // U+007a, U+00df, U+6c34, U+1d10b
|
||||
std::cout << "Byte length: " << utf8.size() << '\n';
|
||||
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
|
||||
std::cout << "Character length: " << conv.from_bytes(utf8).size() << '\n';
|
||||
}
|
||||
37
Task/String-length/C++/string-length-5.cpp
Normal file
37
Task/String-length/C++/string-length-5.cpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include <cwchar> // for mbstate_t
|
||||
#include <locale>
|
||||
|
||||
// give the character length for a given named locale
|
||||
std::size_t char_length(std::string const& text, char const* locale_name)
|
||||
{
|
||||
// locales work on pointers; get length and data from string and
|
||||
// then don't touch the original string any more, to avoid
|
||||
// invalidating the data pointer
|
||||
std::size_t len = text.length();
|
||||
char const* input = text.data();
|
||||
|
||||
// get the named locale
|
||||
std::locale loc(locale_name);
|
||||
|
||||
// get the conversion facet of the locale
|
||||
typedef std::codecvt<wchar_t, char, std::mbstate_t> cvt_type;
|
||||
cvt_type const& cvt = std::use_facet<cvt_type>(loc);
|
||||
|
||||
// allocate buffer for conversion destination
|
||||
std::size_t bufsize = cvt.max_length()*len;
|
||||
wchar_t* destbuf = new wchar_t[bufsize];
|
||||
wchar_t* dest_end;
|
||||
|
||||
// do the conversion
|
||||
mbstate_t state = mbstate_t();
|
||||
cvt.in(state, input, input+len, input, destbuf, destbuf+bufsize, dest_end);
|
||||
|
||||
// determine the length of the converted sequence
|
||||
std::size_t length = dest_end - destbuf;
|
||||
|
||||
// get rid of the buffer
|
||||
delete[] destbuf;
|
||||
|
||||
// return the result
|
||||
return length;
|
||||
}
|
||||
10
Task/String-length/C++/string-length-6.cpp
Normal file
10
Task/String-length/C++/string-length-6.cpp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
// Tür (German for door) in UTF8
|
||||
std::cout << char_length("\x54\xc3\xbc\x72", "de_DE.utf8") << "\n"; // outputs 3
|
||||
|
||||
// Tür in ISO-8859-1
|
||||
std::cout << char_length("\x54\xfc\x72", "de_DE") << "\n"; // outputs 3
|
||||
}
|
||||
9
Task/String-length/C/string-length-1.c
Normal file
9
Task/String-length/C/string-length-1.c
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char *string = "Hello, world!";
|
||||
size_t length = strlen(string);
|
||||
|
||||
return 0;
|
||||
}
|
||||
10
Task/String-length/C/string-length-2.c
Normal file
10
Task/String-length/C/string-length-2.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
int main(void)
|
||||
{
|
||||
const char *string = "Hello, world!";
|
||||
size_t length = 0;
|
||||
|
||||
const char *p = string;
|
||||
while (*p++ != '\0') length++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
9
Task/String-length/C/string-length-3.c
Normal file
9
Task/String-length/C/string-length-3.c
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char s[] = "Hello, world!";
|
||||
size_t length = sizeof s - 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
14
Task/String-length/C/string-length-4.c
Normal file
14
Task/String-length/C/string-length-4.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
wchar_t *s = L"\x304A\x306F\x3088\x3046"; /* Japanese hiragana ohayou */
|
||||
size_t length;
|
||||
|
||||
length = wcslen(s);
|
||||
printf("Length in characters = %d\n", length);
|
||||
printf("Length in bytes = %d\n", sizeof(s) * sizeof(wchar_t));
|
||||
|
||||
return 0;
|
||||
}
|
||||
13
Task/String-length/C/string-length-5.c
Normal file
13
Task/String-length/C/string-length-5.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <locale.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
setlocale(LC_CTYPE, "");
|
||||
char moose[] = "møøse";
|
||||
printf("bytes: %d\n", sizeof(moose) - 1);
|
||||
printf("chars: %d\n", (int)mbstowcs(0, moose, 0));
|
||||
|
||||
return 0;
|
||||
}
|
||||
6
Task/String-length/Clean/string-length.clean
Normal file
6
Task/String-length/Clean/string-length.clean
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import StdEnv
|
||||
|
||||
strlen :: String -> Int
|
||||
strlen string = size string
|
||||
|
||||
Start = strlen "Hello, world!"
|
||||
2
Task/String-length/Clojure/string-length-1.clj
Normal file
2
Task/String-length/Clojure/string-length-1.clj
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(count (.getBytes "Hello, world!")) ; 13 bytes
|
||||
(count (.getBytes "π")) ; two bytes
|
||||
1
Task/String-length/Clojure/string-length-2.clj
Normal file
1
Task/String-length/Clojure/string-length-2.clj
Normal file
|
|
@ -0,0 +1 @@
|
|||
(count "Hello, world!")
|
||||
1
Task/String-length/ColdFusion/string-length-1.cfm
Normal file
1
Task/String-length/ColdFusion/string-length-1.cfm
Normal file
|
|
@ -0,0 +1 @@
|
|||
#len("Hello World")#
|
||||
1
Task/String-length/ColdFusion/string-length-2.cfm
Normal file
1
Task/String-length/ColdFusion/string-length-2.cfm
Normal file
|
|
@ -0,0 +1 @@
|
|||
#len("Hello World")#
|
||||
1
Task/String-length/Common-Lisp/string-length-1.lisp
Normal file
1
Task/String-length/Common-Lisp/string-length-1.lisp
Normal file
|
|
@ -0,0 +1 @@
|
|||
(length (sb-ext:string-to-octets "Hello Wørld"))
|
||||
1
Task/String-length/Common-Lisp/string-length-2.lisp
Normal file
1
Task/String-length/Common-Lisp/string-length-2.lisp
Normal file
|
|
@ -0,0 +1 @@
|
|||
(length "Hello World")
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
MODULE TestLen;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
PROCEDURE DoCharLength*;
|
||||
VAR s: ARRAY 16 OF CHAR; len: INTEGER;
|
||||
BEGIN
|
||||
s := "møøse";
|
||||
len := LEN(s$);
|
||||
Out.String("s: "); Out.String(s); Out.Ln;
|
||||
Out.String("Length of characters: "); Out.Int(len, 0); Out.Ln
|
||||
END DoCharLength;
|
||||
|
||||
END TestLen.
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
MODULE TestLen;
|
||||
|
||||
IMPORT Out;
|
||||
|
||||
PROCEDURE DoByteLength*;
|
||||
VAR s: ARRAY 16 OF CHAR; len, v: INTEGER;
|
||||
BEGIN
|
||||
s := "møøse";
|
||||
len := LEN(s$);
|
||||
v := SIZE(CHAR) * len;
|
||||
Out.String("s: "); Out.String(s); Out.Ln;
|
||||
Out.String("Length of characters in bytes: "); Out.Int(v, 0); Out.Ln
|
||||
END DoByteLength;
|
||||
|
||||
END TestLen.
|
||||
31
Task/String-length/D/string-length-1.d
Normal file
31
Task/String-length/D/string-length-1.d
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import std.stdio;
|
||||
|
||||
void showByteLen(T)(T[] str) {
|
||||
writefln("Byte length: %2d - %(%02x%)",
|
||||
str.length * T.sizeof, cast(ubyte[])str);
|
||||
}
|
||||
|
||||
void main() {
|
||||
string s1a = "møøse"; // UTF-8
|
||||
showByteLen(s1a);
|
||||
wstring s1b = "møøse"; // UTF-16
|
||||
showByteLen(s1b);
|
||||
dstring s1c = "møøse"; // UTF-32
|
||||
showByteLen(s1c);
|
||||
writeln();
|
||||
|
||||
string s2a = "𝔘𝔫𝔦𝔠𝔬𝔡𝔢";
|
||||
showByteLen(s2a);
|
||||
wstring s2b = "𝔘𝔫𝔦𝔠𝔬𝔡𝔢";
|
||||
showByteLen(s2b);
|
||||
dstring s2c = "𝔘𝔫𝔦𝔠𝔬𝔡𝔢";
|
||||
showByteLen(s2c);
|
||||
writeln();
|
||||
|
||||
string s3a = "J̲o̲s̲é̲";
|
||||
showByteLen(s3a);
|
||||
wstring s3b = "J̲o̲s̲é̲";
|
||||
showByteLen(s3b);
|
||||
dstring s3c = "J̲o̲s̲é̲";
|
||||
showByteLen(s3c);
|
||||
}
|
||||
31
Task/String-length/D/string-length-2.d
Normal file
31
Task/String-length/D/string-length-2.d
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import std.stdio, std.range, std.conv;
|
||||
|
||||
void showCodePointsLen(T)(T[] str) {
|
||||
writefln("Character length: %2d - %(%x %)",
|
||||
str.walkLength(), cast(uint[])to!(dchar[])(str));
|
||||
}
|
||||
|
||||
void main() {
|
||||
string s1a = "møøse"; // UTF-8
|
||||
showCodePointsLen(s1a);
|
||||
wstring s1b = "møøse"; // UTF-16
|
||||
showCodePointsLen(s1b);
|
||||
dstring s1c = "møøse"; // UTF-32
|
||||
showCodePointsLen(s1c);
|
||||
writeln();
|
||||
|
||||
string s2a = "𝔘𝔫𝔦𝔠𝔬𝔡𝔢";
|
||||
showCodePointsLen(s2a);
|
||||
wstring s2b = "𝔘𝔫𝔦𝔠𝔬𝔡𝔢";
|
||||
showCodePointsLen(s2b);
|
||||
dstring s2c = "𝔘𝔫𝔦𝔠𝔬𝔡𝔢";
|
||||
showCodePointsLen(s2c);
|
||||
writeln();
|
||||
|
||||
string s3a = "J̲o̲s̲é̲";
|
||||
showCodePointsLen(s3a);
|
||||
wstring s3b = "J̲o̲s̲é̲";
|
||||
showCodePointsLen(s3b);
|
||||
dstring s3c = "J̲o̲s̲é̲";
|
||||
showCodePointsLen(s3c);
|
||||
}
|
||||
1
Task/String-length/Dc/string-length.dc
Normal file
1
Task/String-length/Dc/string-length.dc
Normal file
|
|
@ -0,0 +1 @@
|
|||
[abcde]Zp
|
||||
1
Task/String-length/E/string-length.e
Normal file
1
Task/String-length/E/string-length.e
Normal file
|
|
@ -0,0 +1 @@
|
|||
"Hello World".size()
|
||||
1
Task/String-length/Euphoria/string-length.euphoria
Normal file
1
Task/String-length/Euphoria/string-length.euphoria
Normal file
|
|
@ -0,0 +1 @@
|
|||
print(1,length("Hello World"))
|
||||
2
Task/String-length/Factor/string-length-1.factor
Normal file
2
Task/String-length/Factor/string-length-1.factor
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: string-byte-length ( string -- n ) [ code-point-length ] map-sum ;
|
||||
: string-byte-length-2 ( string -- n ) utf8 encode length ;
|
||||
1
Task/String-length/Factor/string-length-2.factor
Normal file
1
Task/String-length/Factor/string-length-2.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
length
|
||||
14
Task/String-length/Fantom/string-length-1.fantom
Normal file
14
Task/String-length/Fantom/string-length-1.fantom
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
fansh> c := "møøse"
|
||||
møøse
|
||||
fansh> c.toBuf.size // find the byte length of the string in default (UTF8) encoding
|
||||
7
|
||||
fansh> c.toBuf.toHex // display UTF8 representation
|
||||
6dc3b8c3b87365
|
||||
fansh> c.toBuf(Charset.utf16LE).size // byte length in UTF16 little-endian
|
||||
10
|
||||
fansh> c.toBuf(Charset.utf16LE).toHex // display as UTF16 little-endian
|
||||
6d00f800f80073006500
|
||||
fansh> c.toBuf(Charset.utf16BE).size // byte length in UTF16 big-endian
|
||||
10
|
||||
fansh> c.toBuf(Charset.utf16BE).toHex // display as UTF16 big-endian
|
||||
006d00f800f800730065
|
||||
4
Task/String-length/Fantom/string-length-2.fantom
Normal file
4
Task/String-length/Fantom/string-length-2.fantom
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fansh> c := "møøse"
|
||||
møøse
|
||||
fansh> c.size
|
||||
5
|
||||
3
Task/String-length/Forth/string-length-1.fth
Normal file
3
Task/String-length/Forth/string-length-1.fth
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
CREATE s ," Hello world" \ create string "s"
|
||||
s C@ ( -- length=11 )
|
||||
s COUNT ( addr len ) \ convert to a stack string, described below
|
||||
2
Task/String-length/Forth/string-length-2.fth
Normal file
2
Task/String-length/Forth/string-length-2.fth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
S" string" ( addr len)
|
||||
DUP . \ 6
|
||||
9
Task/String-length/Forth/string-length-3.fth
Normal file
9
Task/String-length/Forth/string-length-3.fth
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
2 base !
|
||||
: utf8+ ( str -- str )
|
||||
begin
|
||||
char+
|
||||
dup c@
|
||||
11000000 and
|
||||
10000000 <>
|
||||
until ;
|
||||
decimal
|
||||
8
Task/String-length/Forth/string-length-4.fth
Normal file
8
Task/String-length/Forth/string-length-4.fth
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
: count-utf8 ( zstr -- n )
|
||||
0
|
||||
begin
|
||||
swap dup c@
|
||||
while
|
||||
utf8+
|
||||
swap 1+
|
||||
repeat drop ;
|
||||
3
Task/String-length/GAP/string-length.gap
Normal file
3
Task/String-length/GAP/string-length.gap
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Length("abc");
|
||||
# or same result with
|
||||
Size("abc");
|
||||
2
Task/String-length/GW-BASIC/string-length.gw-basic
Normal file
2
Task/String-length/GW-BASIC/string-length.gw-basic
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
10 INPUT A$
|
||||
20 PRINT LEN(A$)
|
||||
12
Task/String-length/Go/string-length-1.go
Normal file
12
Task/String-length/Go/string-length-1.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
m := "møøse"
|
||||
u := "𝔘𝔫𝔦𝔠𝔬𝔡𝔢"
|
||||
j := "J̲o̲s̲é̲"
|
||||
fmt.Printf("%d %s % x\n", len(m), m, m)
|
||||
fmt.Printf("%d %s %x\n", len(u), u, u)
|
||||
fmt.Printf("%d %s % x\n", len(j), j, j)
|
||||
}
|
||||
15
Task/String-length/Go/string-length-2.go
Normal file
15
Task/String-length/Go/string-length-2.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := "møøse"
|
||||
u := "𝔘𝔫𝔦𝔠𝔬𝔡𝔢"
|
||||
j := "J̲o̲s̲é̲"
|
||||
fmt.Printf("%d %s %x\n", utf8.RuneCountInString(m), m, []rune(m))
|
||||
fmt.Printf("%d %s %x\n", utf8.RuneCountInString(u), u, []rune(u))
|
||||
fmt.Printf("%d %s %x\n", utf8.RuneCountInString(j), j, []rune(j))
|
||||
}
|
||||
30
Task/String-length/Go/string-length-3.go
Normal file
30
Task/String-length/Go/string-length-3.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := "møøse"
|
||||
u := "𝔘𝔫𝔦𝔠𝔬𝔡𝔢"
|
||||
j := "J̲o̲s̲é̲"
|
||||
fmt.Printf("%d %s %x\n", grLen(m), m, []rune(m))
|
||||
fmt.Printf("%d %s %x\n", grLen(u), u, []rune(u))
|
||||
fmt.Printf("%d %s %x\n", grLen(j), j, []rune(j))
|
||||
}
|
||||
|
||||
func grLen(s string) int {
|
||||
if len(s) == 0 {
|
||||
return 0
|
||||
}
|
||||
gr := 1
|
||||
_, s1 := utf8.DecodeRuneInString(s)
|
||||
for _, r := range s[s1:] {
|
||||
if !unicode.Is(unicode.Mn, r) {
|
||||
gr++
|
||||
}
|
||||
}
|
||||
return gr
|
||||
}
|
||||
1
Task/String-length/Groovy/string-length.groovy
Normal file
1
Task/String-length/Groovy/string-length.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
println "Hello World!".size()
|
||||
11
Task/String-length/Haskell/string-length-1.hs
Normal file
11
Task/String-length/Haskell/string-length-1.hs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import Data.Encoding
|
||||
import Data.ByteString as B
|
||||
|
||||
strUTF8 :: ByteString
|
||||
strUTF8 = encode UTF8 "Hello World!"
|
||||
|
||||
strUTF32 :: ByteString
|
||||
strUTF32 = encode UTF32 "Hello World!"
|
||||
|
||||
strlenUTF8 = B.length strUTF8
|
||||
strlenUTF32 = B.length strUTF32
|
||||
1
Task/String-length/Haskell/string-length-2.hs
Normal file
1
Task/String-length/Haskell/string-length-2.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
strlen = length "Hello, world!"
|
||||
1
Task/String-length/HicEst/string-length.hicest
Normal file
1
Task/String-length/HicEst/string-length.hicest
Normal file
|
|
@ -0,0 +1 @@
|
|||
LEN("1 character == 1 byte") ! 21
|
||||
1
Task/String-length/IDL/string-length-1.idl
Normal file
1
Task/String-length/IDL/string-length-1.idl
Normal file
|
|
@ -0,0 +1 @@
|
|||
length = strlen("Hello, world!")
|
||||
1
Task/String-length/IDL/string-length-2.idl
Normal file
1
Task/String-length/IDL/string-length-2.idl
Normal file
|
|
@ -0,0 +1 @@
|
|||
length = strlen("Hello, world!")
|
||||
1
Task/String-length/Icon/string-length.icon
Normal file
1
Task/String-length/Icon/string-length.icon
Normal file
|
|
@ -0,0 +1 @@
|
|||
length := *s
|
||||
1
Task/String-length/Io/string-length-1.io
Normal file
1
Task/String-length/Io/string-length-1.io
Normal file
|
|
@ -0,0 +1 @@
|
|||
"møøse" sizeInBytes
|
||||
1
Task/String-length/Io/string-length-2.io
Normal file
1
Task/String-length/Io/string-length-2.io
Normal file
|
|
@ -0,0 +1 @@
|
|||
"møøse" size
|
||||
2
Task/String-length/J/string-length-1.j
Normal file
2
Task/String-length/J/string-length-1.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# 'møøse'
|
||||
7
|
||||
2
Task/String-length/J/string-length-2.j
Normal file
2
Task/String-length/J/string-length-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#7 u: 'møøse'
|
||||
5
|
||||
2
Task/String-length/Java/string-length-1.java
Normal file
2
Task/String-length/Java/string-length-1.java
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
String s = "Hello, world!";
|
||||
int byteCount = s.length() * 2;
|
||||
3
Task/String-length/Java/string-length-2.java
Normal file
3
Task/String-length/Java/string-length-2.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
String s = "Hello, world!";
|
||||
int byteCountUTF16 = s.getBytes("UTF-16").length;
|
||||
int byteCountUTF8 = s.getBytes("UTF-8").length;
|
||||
2
Task/String-length/Java/string-length-3.java
Normal file
2
Task/String-length/Java/string-length-3.java
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
String s = "Hello, world!";
|
||||
int not_really_the_length = s.length(); // XXX: does not (always) count Unicode characters (code points)!
|
||||
3
Task/String-length/Java/string-length-4.java
Normal file
3
Task/String-length/Java/string-length-4.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
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
|
||||
19
Task/String-length/Java/string-length-5.java
Normal file
19
Task/String-length/Java/string-length-5.java
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
2
Task/String-length/JavaScript/string-length-1.js
Normal file
2
Task/String-length/JavaScript/string-length-1.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var s = "Hello, world!";
|
||||
var byteCount = s.length * 2; //26
|
||||
5
Task/String-length/JavaScript/string-length-2.js
Normal file
5
Task/String-length/JavaScript/string-length-2.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
var str1 = "Hello, world!";
|
||||
var len1 = str1.length; //13
|
||||
|
||||
var str2 = "\uD834\uDD2A"; //U+1D12A represented by a UTF-16 surrogate pair
|
||||
var len2 = str2.length; //2
|
||||
2
Task/String-length/JudoScript/string-length-1.judoscript
Normal file
2
Task/String-length/JudoScript/string-length-1.judoscript
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
//Store length of hello world in length and print it
|
||||
. length = "Hello World".length();
|
||||
2
Task/String-length/JudoScript/string-length-2.judoscript
Normal file
2
Task/String-length/JudoScript/string-length-2.judoscript
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
//Store length of hello world in length and print it
|
||||
. length = "Hello World".length()
|
||||
1
Task/String-length/LSE64/string-length-1.lse64
Normal file
1
Task/String-length/LSE64/string-length-1.lse64
Normal file
|
|
@ -0,0 +1 @@
|
|||
" Hello world" @ 1 + 8 * , # 96 = (11+1)*(size of a cell) = 12*8
|
||||
1
Task/String-length/LSE64/string-length-2.lse64
Normal file
1
Task/String-length/LSE64/string-length-2.lse64
Normal file
|
|
@ -0,0 +1 @@
|
|||
" Hello world" @ , # 11
|
||||
3
Task/String-length/Logo/string-length.logo
Normal file
3
Task/String-length/Logo/string-length.logo
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
print count "|Hello World| ; 11
|
||||
print count "møøse ; 5
|
||||
print char 248 ; ø - implies ISO-Latin character set
|
||||
2
Task/String-length/Lua/string-length-1.lua
Normal file
2
Task/String-length/Lua/string-length-1.lua
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
str = "Hello world"
|
||||
length = #str
|
||||
2
Task/String-length/Lua/string-length-2.lua
Normal file
2
Task/String-length/Lua/string-length-2.lua
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
str = "Hello world"
|
||||
length = string.len(str)
|
||||
2
Task/String-length/Lua/string-length-3.lua
Normal file
2
Task/String-length/Lua/string-length-3.lua
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
str = "Hello world"
|
||||
length = #str
|
||||
2
Task/String-length/Lua/string-length-4.lua
Normal file
2
Task/String-length/Lua/string-length-4.lua
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
str = "Hello world"
|
||||
length = string.len(str)
|
||||
5
Task/String-length/MATLAB/string-length-1.m
Normal file
5
Task/String-length/MATLAB/string-length-1.m
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
>> length('møøse')
|
||||
|
||||
ans =
|
||||
|
||||
5
|
||||
5
Task/String-length/MATLAB/string-length-2.m
Normal file
5
Task/String-length/MATLAB/string-length-2.m
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
>> numel(dec2hex('møøse'))
|
||||
|
||||
ans =
|
||||
|
||||
10
|
||||
1
Task/String-length/MAXScript/string-length.maxscript
Normal file
1
Task/String-length/MAXScript/string-length.maxscript
Normal file
|
|
@ -0,0 +1 @@
|
|||
"Hello world".count
|
||||
1
Task/String-length/Mathematica/string-length-1.math
Normal file
1
Task/String-length/Mathematica/string-length-1.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
StringLength["Hello world"]
|
||||
1
Task/String-length/Mathematica/string-length-2.math
Normal file
1
Task/String-length/Mathematica/string-length-2.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
StringByteCount["Hello world"]
|
||||
3
Task/String-length/Maxima/string-length.maxima
Normal file
3
Task/String-length/Maxima/string-length.maxima
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
s: "the quick brown fox jumps over the lazy dog";
|
||||
slength(s);
|
||||
/* 43 */
|
||||
6
Task/String-length/Metafont/string-length.metafont
Normal file
6
Task/String-length/Metafont/string-length.metafont
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
string s;
|
||||
s := "Hello Moose";
|
||||
show length(s); % 11 (ok)
|
||||
s := "Hello Møøse";
|
||||
show length(s); % 13 (number of bytes when the string is UTF-8 encoded,
|
||||
% since ø takes two bytes)
|
||||
9
Task/String-length/Modula-3/string-length-1.mod3
Normal file
9
Task/String-length/Modula-3/string-length-1.mod3
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
MODULE ByteLength EXPORTS Main;
|
||||
|
||||
IMPORT IO, Fmt, Text;
|
||||
|
||||
VAR s: TEXT := "Foo bar baz";
|
||||
|
||||
BEGIN
|
||||
IO.Put("Byte length of s: " & Fmt.Int((Text.Length(s) * BYTESIZE(s))) & "\n");
|
||||
END ByteLength.
|
||||
9
Task/String-length/Modula-3/string-length-2.mod3
Normal file
9
Task/String-length/Modula-3/string-length-2.mod3
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
MODULE StringLength EXPORTS Main;
|
||||
|
||||
IMPORT IO, Fmt, Text;
|
||||
|
||||
VAR s: TEXT := "Foo bar baz";
|
||||
|
||||
BEGIN
|
||||
IO.Put("String length of s: " & Fmt.Int(Text.Length(s)) & "\n");
|
||||
END StringLength.
|
||||
2
Task/String-length/Nemerle/string-length-1.nemerle
Normal file
2
Task/String-length/Nemerle/string-length-1.nemerle
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def message = "How long am I anyways?";
|
||||
def charlength = message.Length;
|
||||
4
Task/String-length/Nemerle/string-length-2.nemerle
Normal file
4
Task/String-length/Nemerle/string-length-2.nemerle
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
using System.Text;
|
||||
|
||||
def message = "How long am I anyways?";
|
||||
def bytelength = Encoding.Unicode.GetByteCount(message);
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue