String length: Difference between revisions
Content added Content deleted
m (→{{header|UNIX Shell}}: Add note about bytes vs characters in implementations) |
|||
Line 2,054: | Line 2,054: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang="m2000 interpreter"> |
<syntaxhighlight lang="m2000 interpreter"> |
||
module String_length { |
|||
A$=format$("J\u0332o\u0332s\u0332e\u0301\u0332") |
A$=format$("J\u0332o\u0332s\u0332e\u0301\u0332") |
||
Print Len(A$) = 9 ' true Utf-16LE |
|||
Print Len |
Print Len(A$) = 9 ' true Utf-16LE |
||
Print Len.Disp(A$) = 4 \\ display length |
|||
Buffer Clear Mem as Byte*100 |
Buffer Clear Mem as Byte*100 |
||
\\ Write at memory at offset 0 or address Mem(0) |
\\ Write at memory at offset 0 or address Mem(0) |
||
Return Mem, 0:=A$ |
|||
Return Mem, 0:=A$ |
|||
Print Eval$(Mem, 0, 18) |
|||
For i=0 to 17 step 2 |
For i=0 to 17 step 2 |
||
\\ print hex value and character |
\\ print hex value and character |
||
Hex Eval(Mem, i as integer), ChrCode$(Eval(Mem, i as integer)) |
Hex Eval(Mem, i as integer), ChrCode$(Eval(Mem, i as integer)) |
||
Next i |
Next i |
||
Document B$=A$ |
Document B$=A$ |
||
\\ encode to utf-8 with BOM (3 bytes 0xEF,0xBB,0xBF) |
\\ encode to utf-8 with BOM (3 bytes 0xEF,0xBB,0xBF) |
||
⚫ | |||
Save.Doc B$, "Checklen.doc", 2 |
|||
Print Filelen("Checklen.doc")=17 |
|||
\\ So length is 14 bytes + 3 the BOM |
\\ So length is 14 bytes + 3 the BOM |
||
⚫ | |||
Print len(Mem)=17 // len works for buffers too - unit byte |
|||
// version 12 can handle strings without suffix $ |
|||
C=eval$(mem, 3, 14) // from 4th byte get 14 bytes in a string |
|||
Print len(C)*2=14 ' bytes // len()) for strings return double type of words (can return 0.5) |
|||
C=string$(C as utf8dec) ' decode bytes from utf8 to utf16LE |
|||
Print len(C)=9, C=A$, Len.Disp(C)=4 |
|||
Print C |
|||
Report 2, C // proportional print on console - for text center justified rendering (2 - center) |
|||
} |
|||
String_length |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||