all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

View 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.

View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View file

@ -0,0 +1 @@
$length:=Length("Hello, world!")

View 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
))

View 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))

View 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

View file

@ -0,0 +1,2 @@
#!/usr/bin/awk -f
{print"The length of this line is "length($0)}

View file

@ -0,0 +1 @@
myStrVar.length()

View file

@ -0,0 +1,2 @@
Str : String := "Hello World";
Length : constant Natural := Str'Size / 8;

View 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;

View file

@ -0,0 +1 @@
count of "Hello World"

View 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

View file

@ -0,0 +1 @@
count of "Hello World"

View file

@ -0,0 +1 @@
count "Hello World"

View file

@ -0,0 +1 @@
Msgbox % StrLen("Hello World")

View file

@ -0,0 +1,3 @@
String := "Hello World"
StringLen, Length, String
Msgbox % Length

View file

@ -0,0 +1,2 @@
INPUT a$
PRINT LEN(a$)

View file

@ -0,0 +1,2 @@
10 INPUT a$
20 PRINT LEN(a$)

View file

@ -0,0 +1,2 @@
INPUT text$
PRINT LEN(text$)

View 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$)

View 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

View file

@ -0,0 +1,7 @@
(ByteLength=
length
. @(!arg:? [?length)
& !length
);
out$ByteLength$𝔘𝔫𝔦𝔠𝔬𝔡𝔢

View file

@ -0,0 +1,16 @@
(CharacterLength=
length c
. 0:?length
& @( !arg
: ?
( %?c
& utf$!c:?k
& 1+!length:?length
& ~
)
?
)
| !length
);
out$CharacterLength$𝔘𝔫𝔦𝔠𝔬𝔡𝔢

View file

@ -0,0 +1,14 @@
(CharacterLength=
length c p
. 0:?length:?p
& @( !arg
: ?
( [!p %?c
& utf$!c:?k
& 1+!length:?length
)
([?p&~)
?
)
| !length
);

View 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);
}

View 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
}

View 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();
}

View 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';
}

View 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;
}

View 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
}

View file

@ -0,0 +1,9 @@
#include <string.h>
int main(void)
{
const char *string = "Hello, world!";
size_t length = strlen(string);
return 0;
}

View 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;
}

View file

@ -0,0 +1,9 @@
#include <stdlib.h>
int main(void)
{
char s[] = "Hello, world!";
size_t length = sizeof s - 1;
return 0;
}

View 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;
}

View 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;
}

View file

@ -0,0 +1,6 @@
import StdEnv
strlen :: String -> Int
strlen string = size string
Start = strlen "Hello, world!"

View file

@ -0,0 +1,2 @@
(count (.getBytes "Hello, world!")) ; 13 bytes
(count (.getBytes "π")) ; two bytes

View file

@ -0,0 +1 @@
(count "Hello, world!")

View file

@ -0,0 +1 @@
#len("Hello World")#

View file

@ -0,0 +1 @@
#len("Hello World")#

View file

@ -0,0 +1 @@
(length (sb-ext:string-to-octets "Hello Wørld"))

View file

@ -0,0 +1 @@
(length "Hello World")

View file

@ -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.

View file

@ -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.

View 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);
}

View 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);
}

View file

@ -0,0 +1 @@
[abcde]Zp

View file

@ -0,0 +1 @@
"Hello World".size()

View file

@ -0,0 +1 @@
print(1,length("Hello World"))

View file

@ -0,0 +1,2 @@
: string-byte-length ( string -- n ) [ code-point-length ] map-sum ;
: string-byte-length-2 ( string -- n ) utf8 encode length ;

View file

@ -0,0 +1 @@
length

View 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

View file

@ -0,0 +1,4 @@
fansh> c := "møøse"
møøse
fansh> c.size
5

View 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

View file

@ -0,0 +1,2 @@
S" string" ( addr len)
DUP . \ 6

View file

@ -0,0 +1,9 @@
2 base !
: utf8+ ( str -- str )
begin
char+
dup c@
11000000 and
10000000 <>
until ;
decimal

View file

@ -0,0 +1,8 @@
: count-utf8 ( zstr -- n )
0
begin
swap dup c@
while
utf8+
swap 1+
repeat drop ;

View file

@ -0,0 +1,3 @@
Length("abc");
# or same result with
Size("abc");

View file

@ -0,0 +1,2 @@
10 INPUT A$
20 PRINT LEN(A$)

View 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)
}

View 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))
}

View 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
}

View file

@ -0,0 +1 @@
println "Hello World!".size()

View 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

View file

@ -0,0 +1 @@
strlen = length "Hello, world!"

View file

@ -0,0 +1 @@
LEN("1 character == 1 byte") ! 21

View file

@ -0,0 +1 @@
length = strlen("Hello, world!")

View file

@ -0,0 +1 @@
length = strlen("Hello, world!")

View file

@ -0,0 +1 @@
length := *s

View file

@ -0,0 +1 @@
"møøse" sizeInBytes

View file

@ -0,0 +1 @@
"møøse" size

View file

@ -0,0 +1,2 @@
# 'møøse'
7

View file

@ -0,0 +1,2 @@
#7 u: 'møøse'
5

View file

@ -0,0 +1,2 @@
String s = "Hello, world!";
int byteCount = s.length() * 2;

View file

@ -0,0 +1,3 @@
String s = "Hello, world!";
int byteCountUTF16 = s.getBytes("UTF-16").length;
int byteCountUTF8 = s.getBytes("UTF-8").length;

View 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)!

View 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

View 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);
}
}

View file

@ -0,0 +1,2 @@
var s = "Hello, world!";
var byteCount = s.length * 2; //26

View 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

View file

@ -0,0 +1,2 @@
//Store length of hello world in length and print it
. length = "Hello World".length();

View file

@ -0,0 +1,2 @@
//Store length of hello world in length and print it
. length = "Hello World".length()

View file

@ -0,0 +1 @@
" Hello world" @ 1 + 8 * , # 96 = (11+1)*(size of a cell) = 12*8

View file

@ -0,0 +1 @@
" Hello world" @ , # 11

View file

@ -0,0 +1,3 @@
print count "|Hello World| ; 11
print count "møøse ; 5
print char 248 ; ø - implies ISO-Latin character set

View file

@ -0,0 +1,2 @@
str = "Hello world"
length = #str

View file

@ -0,0 +1,2 @@
str = "Hello world"
length = string.len(str)

View file

@ -0,0 +1,2 @@
str = "Hello world"
length = #str

View file

@ -0,0 +1,2 @@
str = "Hello world"
length = string.len(str)

View file

@ -0,0 +1,5 @@
>> length('møøse')
ans =
5

View file

@ -0,0 +1,5 @@
>> numel(dec2hex('møøse'))
ans =
10

View file

@ -0,0 +1 @@
"Hello world".count

View file

@ -0,0 +1 @@
StringLength["Hello world"]

View file

@ -0,0 +1 @@
StringByteCount["Hello world"]

View file

@ -0,0 +1,3 @@
s: "the quick brown fox jumps over the lazy dog";
slength(s);
/* 43 */

View 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)

View 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.

View 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.

View file

@ -0,0 +1,2 @@
def message = "How long am I anyways?";
def charlength = message.Length;

View 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