2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,13 +1,25 @@
|
|||
{{omit from|GUISS|Can use Microsoft Word document properties, but this might not be accurate}}
|
||||
{{omit from|Openscad}}
|
||||
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. <br>
|
||||
|
||||
;Task:
|
||||
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>. <br>
|
||||
|
||||
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.
|
||||
<br><br>
|
||||
|
||||
{{Template:Strings}}
|
||||
<br><br>
|
||||
|
|
|
|||
25
Task/String-length/360-Assembly/string-length.360
Normal file
25
Task/String-length/360-Assembly/string-length.360
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
* String length 06/07/2016
|
||||
LEN CSECT
|
||||
USING LEN,15 base register
|
||||
LA 1,L'C length of C
|
||||
XDECO 1,PG
|
||||
XPRNT PG,12
|
||||
LA 1,L'H length of H
|
||||
XDECO 1,PG
|
||||
XPRNT PG,12
|
||||
LA 1,L'F length of F
|
||||
XDECO 1,PG
|
||||
XPRNT PG,12
|
||||
LA 1,L'D length of D
|
||||
XDECO 1,PG
|
||||
XPRNT PG,12
|
||||
LA 1,L'PG length of PG
|
||||
XDECO 1,PG
|
||||
XPRNT PG,12
|
||||
BR 14 exit length
|
||||
C DS C character 1
|
||||
H DS H half word 2
|
||||
F DS F full word 4
|
||||
D DS D double word 8
|
||||
PG DS CL12 string 12
|
||||
END LEN
|
||||
6
Task/String-length/Elena/string-length-1.elena
Normal file
6
Task/String-length/Elena/string-length-1.elena
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#var s := "Hello, world!". // UTF-8 literal
|
||||
#var ws := "Привет мир!"w. // UTF-16 literal
|
||||
|
||||
#var s_length := s length. // Number of UTF-8 characters
|
||||
#var ws_length := ws length. // Number of UTF-16 characters
|
||||
#var u_length := ws toArray length. //Number of UTF-32 characters
|
||||
2
Task/String-length/Elena/string-length-2.elena
Normal file
2
Task/String-length/Elena/string-length-2.elena
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#var s_byte_length := s toByteArray length. // Number of bytes
|
||||
#var ws_byte_length := ws toByteArray length. // Number of bytes
|
||||
21
Task/String-length/MIPS-Assembly/string-length.mips
Normal file
21
Task/String-length/MIPS-Assembly/string-length.mips
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
.data
|
||||
#.asciiz automatically adds the NULL terminator character, \0 for us.
|
||||
string: .asciiz "Nice string you got there!"
|
||||
|
||||
.text
|
||||
main:
|
||||
la $a1,string #load the beginning address of the string.
|
||||
|
||||
loop:
|
||||
lb $a2,($a1) #load byte (i.e. the char) at $a1 into $a2
|
||||
addi $a1,$a1,1 #increment $a1
|
||||
beqz $a2,exit_procedure #see if we've hit the NULL char yet
|
||||
addi $a0,$a0,1 #increment counter
|
||||
j loop #back to start
|
||||
|
||||
exit_procedure:
|
||||
li $v0,1 #set syscall to print integer
|
||||
syscall
|
||||
|
||||
li $v0,10 #set syscall to cleanly exit EXIT_SUCCESS
|
||||
syscall
|
||||
5
Task/String-length/REBOL/string-length-1.rebol
Normal file
5
Task/String-length/REBOL/string-length-1.rebol
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
;; r2
|
||||
length? "møøse"
|
||||
|
||||
;; r3
|
||||
length? to-binary "møøse"
|
||||
2
Task/String-length/REBOL/string-length-2.rebol
Normal file
2
Task/String-length/REBOL/string-length-2.rebol
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
;; r3
|
||||
length? "møøse"
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
text: "møøse"
|
||||
print rejoin ["Byte length for '" text "': " length? text]
|
||||
|
|
@ -1,10 +1,11 @@
|
|||
/*REXX program to show lengths (in bytes/characters) for various strings*/
|
||||
/* 1 */ /*a handy over/under scale.*/
|
||||
/*REXX program displays the lengths (in bytes/characters) for various strings. */
|
||||
/* 1 */ /*a handy-dandy over/under scale.*/
|
||||
/* 123456789012345 */
|
||||
hello = 'Hello, world!' ; say 'length of HELLO is ' length(hello)
|
||||
happy = 'Hello, world! ☺' ; say 'length of HAPPY is ' length(happy)
|
||||
jose = 'José' ; say 'length of JOSE is ' length(jose)
|
||||
nill = '' ; say 'length of NILL is ' length(nill)
|
||||
null = ; say 'length of NULL is ' length(null)
|
||||
sum = 5+1 ; say 'length of SUM is ' length(sum)
|
||||
/*stick a fork in it, we're done.*/
|
||||
hello = 'Hello, world!' ; say 'the length of HELLO is ' length(hello)
|
||||
happy = 'Hello, world! ☺' ; say 'the length of HAPPY is ' length(happy)
|
||||
jose = 'José' ; say 'the length of JOSE is ' length(jose)
|
||||
nill = '' ; say 'the length of NILL is ' length(nill)
|
||||
null = ; say 'the length of NULL is ' length(null)
|
||||
sum = 5+1 ; say 'the length of SUM is ' length(sum)
|
||||
/* [↑] is, of course, 6. */
|
||||
/*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue