1 module viva.types..string;
2 
3 /++
4  + Creates a string of multiple other strings
5  + Params:
6  +      values = The values that should be combined into a string
7  + Returns: The new string
8  +/
9 public string str(T...)(T values) pure nothrow @safe
10 {
11     string result = "";
12     if (values.length > 1)
13         foreach (value; values)
14             result ~= value;
15 
16     return result;
17 }
18 
19 /++
20  + Appends additional data to a string
21  + Params:
22  +      s = The string to append the value to
23  +      a = The value to be appended
24  + Returns: `s` with `a` appended
25  +/
26 public string append(T)(ref string s, T a) pure nothrow @safe
27 {
28     import std.conv : to;
29 
30     string b = a.to!string;
31     s = s ~ b;
32 
33     return s;
34 }
35 
36 /++
37  + Get the FNV-1a hash of the string
38  + Example:
39  +      ```d
40  +       println(str.hash);
41  +      ```
42  + Params:
43  +      s = The string that should be hashed
44  +/
45 public uint hash(ref string s) pure nothrow @safe
46 {
47     uint hash = 2_166_136_261;
48 
49     foreach (c; s)
50     {
51         hash ^= c;
52         hash *= 16_777_619;
53     }
54 
55     return hash;
56 }
57 
58 /++
59  + Get the FNV-1a hash of the string
60  + Example:
61  +      ```d
62  +       println("Hello, World!".hash);
63  +      ```
64  + Params:
65  +      s = The string that should be hashed
66  +/
67 public uint hash(string s) pure nothrow @safe
68 {
69     return hash(s);
70 }
71 
72 /++
73  + Repeats a string `n` times
74  + Params:
75  +      s = The string that should be repeated
76  +      n = The amount of times it should be repeated
77  + Returns: The string repeated `n` times
78  +/
79 public string repeat(ref string s, long n) pure nothrow @safe
80 {
81     string a = "";
82 
83     for (int i = 0; i < n; i++)
84     {
85         a ~= s;
86     }
87 
88     return a;
89 }
90 
91 /++
92  + Repeats a string `n` times
93  + Params:
94  +      s = The string that should be repeated
95  +      n = The amount of times it should be repeated
96  + Returns: The string repeated `n` times
97  +/
98 public string repeat(string s, long n) pure nothrow @safe
99 {
100     return repeat(s, n);
101 }
102 
103 /++
104  + Converts all characters in a givens string to uppercase
105  + Params:
106  +      s = The string to be processed
107  + Returns: The new string
108  +/
109 public string toUpper(ref string s) pure @safe
110 {
111     import viva.types.number : inRange;
112     import std.format : format;
113 
114     char[] a = new char[s.length];
115 
116     int idx;
117     foreach (c; s)
118     {
119         int i = cast(int) c;
120 
121         if (i.inRange(97, 122))
122         {
123             i -= 32;
124         }
125 
126         a[idx] = cast(char) i;
127         idx++;
128     }
129 
130     return format!"%s"(a);
131 }
132 
133 /++
134  + Converts all characters in a givens string to uppercase
135  + Params:
136  +      s = The string to be processed
137  + Returns: The new string
138  +/
139 public string toUpper(string s) pure @safe
140 {
141     return toUpper(s);
142 }
143 
144 /++
145  + Converts all characters in a givens string to lowercase
146  + Params:
147  +      s = The string to be processed
148  + Returns: The new string
149  +/
150 public string toLower(ref string s) @safe
151 {
152     import viva.types.number : inRange;
153     import std.format : format;
154 
155     char[] a = new char[s.length];
156 
157     int idx;
158     foreach (c; s)
159     {
160         int i = cast(int) c;
161 
162         if (i.inRange(65, 90))
163         {
164             i += 32;
165         }
166 
167         a[idx] = cast(char) i;
168         idx++;
169     }
170 
171     return format!"%s"(a);
172 }
173 
174 /++
175  + Converts all characters in a givens string to lowercase
176  + Params:
177  +      s = The string to be processed
178  + Returns: The new string
179  +/
180 public string toLower(string s) @safe
181 {
182     return toLower(s);
183 }
184 
185 /++
186  + Splits a string into an array by a given seperator
187  + Params:
188  +      s = The string to be splitted
189  +      sep = The seperator specifying where to split
190  + Returns: An array of the elements from the splitted string
191  +/
192 public string[] split(ref string s, string sep) @safe
193 {
194     string[] values;
195 
196     foreach (c; s)
197     {
198         // TODO: What if the sep is more than just a char
199     }
200 
201     return values;
202 }
203 
204 /++
205  + Splits a string into an array by a given seperator
206  + Params:
207  +      s = The string to be splitted
208  +      sep = The seperator specifying where to split
209  + Returns: An array of the elements from the splitted string
210  +/
211 public string[] split(string s, string sep) @safe
212 {
213     return split(s, sep);
214 }