How to code Z-Function of string?

WebFreak001 d.forum at webfreak.org
Fri Mar 27 06:57:38 UTC 2020


On Thursday, 26 March 2020 at 20:08:39 UTC, Dennis wrote:
> On Thursday, 26 March 2020 at 19:34:08 UTC, Quantium wrote:
>> 1. How can I make string ONLY char[] (Not immutable)
>
> You can use .dup to make a mutable copy of an array.
>
> ```
> char[] a = "abc".dup;
> ```

extending on this: if you want to work on single unicode 
codepoints instead of utf 8 encoded bytes, you can use:

char[] a = "abc".to!(char[]); // utf8 encoded bytes - single 
bytes may at most cover basic ASCII characters
wchar[] a = "abc".to!(wchar[]); // utf16 encoded bytes - each 
element is ushort size, covers all characters of the Basic 
Multilingual Plane, and otherwise uses surrogate pairs which you 
might want to check for. Emoji span 2 code units for example
dchar[] a = "abc".to!(dchar[]); // utf32 - each element is uint, 
covers all possible code points

however note that code points may not cover all characters in all 
languages. Characters might be composed of multiple code points, 
even relatively innocent looking characters like ä may be 
composited of an umlaut code point and the character 'a'

Basically before doing any operations on your text, I would 
specify and check for rules to restrict what your input can be. 
For example if you only want characters from certain ranges such 
as latin characters, restrict the user from entering anything 
else. std.uni can help you greatly with working with these ranges 
and std.utf can help you decode utf8/utf16/utf32 if you choose 
not to convert to char[], wchar[] or dchar[] before.


More information about the Digitalmars-d-learn mailing list