1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.text.Common;
13 
14 import hunt.Exceptions;
15 
16 import std.algorithm;
17 import std.exception;
18 import std.string;
19 
20 bool equalsIgnoreCase(string s1, string s2) {
21     return icmp(s1, s2) == 0;
22 }
23 
24 bool equals(string s1, string s2) {
25     return s1 == s2;
26 }
27 
28 string substring(string s, int beginIndex, int endIndex = -1) {
29     if (endIndex == -1)
30         endIndex = cast(int) s.length;
31     return s[beginIndex .. endIndex];
32 }
33 
34 string substring(string s, ulong beginIndex, ulong endIndex = -1) {
35     return substring(s, cast(int) beginIndex, cast(int) endIndex);
36 }
37 
38 char charAt(string s, int i) nothrow {
39     return s[i];
40 }
41 
42 char charAt(string s, size_t i) nothrow {
43     return s[i];
44 }
45 
46 bool contains(string items, string item) {
47     return items.canFind(item);
48 }
49 
50 bool contains(T)(T[] items, T item) {
51     return items.canFind(item);
52 }
53 
54 int compareTo(string value, string another) {
55     import std.algorithm.comparison;
56 
57     int len1 = cast(int) value.length;
58     int len2 = cast(int) another.length;
59     int lim = min(len1, len2);
60     // char v1[] = value;
61     // char v2[] = another.value;
62 
63     int k = 0;
64     while (k < lim) {
65         char c1 = value[k];
66         char c2 = another[k];
67         if (c1 != c2) {
68             return c1 - c2;
69         }
70         k++;
71     }
72     return len1 - len2;
73 }
74 
75 bool isEmpty(string str)
76 {
77     return str.length == 0;
78 }