digitalmars.D.learn - How to code Z-Function of string?
- Quantium (8/8) Mar 26 2020 I cannot make a code that is calculating Z-Function of string.
- Dennis (6/12) Mar 26 2020 You can use .dup to make a mutable copy of an array.
- WebFreak001 (22/28) Mar 26 2020 extending on this: if you want to work on single unicode
- MoonlightSentinel (5/8) Mar 26 2020 Yes, but keep in mind that char[] implies UTF8. Hence this won't
I cannot make a code that is calculating Z-Function of string. Main trouble is that string is immutable char[]. 1. How can I make string ONLY char[] (Not immutable) 2. How can I work with some of chars in the stirng, is, for example: string s="abc"; writeln(s[1]); // Should write 'b' or not??? Is it a legit code or it doesn't work ?
Mar 26 2020
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; ```2. How can I work with some of chars in the stirng, is, for example: string s="abc"; writeln(s[1]); // Should write 'b' or not??? Is it a legit code or it doesn't work ?Yes, that works.
Mar 26 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: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.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; ```
Mar 26 2020
On Thursday, 26 March 2020 at 19:34:08 UTC, Quantium wrote:string s="abc"; writeln(s[1]); // Should write 'b' or not??? Is it a legit code or it doesn't work ?Yes, but keep in mind that char[] implies UTF8. Hence this won't work if your input contains e.g. chinese characters. Refer to std.utf (https://dlang.org/phobos/std_utf.html) for some utilities to deal with UTF.
Mar 26 2020