1 /*
2                                     __
3                                    / _|
4   __ _ _   _ _ __ ___  _ __ __ _  | |_ ___  ___ ___
5  / _` | | | | '__/ _ \| '__/ _` | |  _/ _ \/ __/ __|
6 | (_| | |_| | | | (_) | | | (_| | | || (_) \__ \__ \
7  \__,_|\__,_|_|  \___/|_|  \__,_| |_| \___/|___/___/
8 
9 Copyright (C) 2018-2019 Aurora Free Open Source Software.
10 Copyright (C) 2018-2019 Luís Ferreira <luis@aurorafoss.org>
11 
12 This file is part of the Aurora Free Open Source Software. This
13 organization promote free and open source software that you can
14 redistribute and/or modify under the terms of the GNU Lesser General
15 Public License Version 3 as published by the Free Software Foundation or
16 (at your option) any later version approved by the Aurora Free Open Source
17 Software Organization. The license is available in the package root path
18 as 'LICENSE' file. Please review the following information to ensure the
19 GNU Lesser General Public License version 3 requirements will be met:
20 https://www.gnu.org/licenses/lgpl.html .
21 
22 Alternatively, this file may be used under the terms of the GNU General
23 Public License version 3 or later as published by the Free Software
24 Foundation. Please review the following information to ensure the GNU
25 General Public License requirements will be met:
26 http://www.gnu.org/licenses/gpl-3.0.html.
27 
28 NOTE: All products, services or anything associated to trademarks and
29 service marks used or referenced on this file are the property of their
30 respective companies/owners or its subsidiaries. Other names and brands
31 may be claimed as the property of others.
32 
33 For more info about intellectual property visit: aurorafoss.org or
34 directly send an email to: contact (at) aurorafoss.org .
35 */
36 
37 module aurorafw.stdx..string;
38 
39 public import std..string;
40 
41 version (unittest) import aurorafw.unit.assertion;
42 
43 @nogc @safe pure nothrow
44 string substr(string s, ptrdiff_t offset, ptrdiff_t length = -1)
45 {
46 	size_t end = void;
47 	import std.stdio;
48 
49 	if (offset > 0 && offset > s.length)
50 		return "";
51 	if (offset < 0)
52 		offset = 0;
53 	if (length < 0)
54 		end = s.length;
55 	else
56 		end = offset + length;
57 	if (end > s.length)
58 		end = s.length;
59 
60 	return s[offset .. end];
61 }
62 
63 ///
64 @safe pure
65 @("String: substr")
66 unittest
67 {
68 	string s = "Aurora Framework";
69 	assertEquals("urora", s.substr(1, 5));
70 	assertEquals("urora Framework", s.substr(1));
71 	assertEquals(s, s.substr(-1, -1));
72 	assertEquals(s, s.substr(-1));
73 	assertEquals(s, s.substr(0, ptrdiff_t.max));
74 	assertEquals("", s.substr(ptrdiff_t.max));
75 }
76 
77 @nogc @safe pure nothrow
78 bool isAlpha(string str)
79 {
80 	import std.algorithm : all;
81 	import std.uni : isAlpha;
82 
83 	foreach (c; str)
84 		if (!isAlpha(c))
85 			return false;
86 
87 	return true;
88 }
89 
90 ///
91 @safe pure
92 @("String: isAlpha")
93 unittest
94 {
95 	assertTrue(isAlpha("tunaisgood"));
96 	assertFalse(isAlpha("tun41s900d"));
97 }
98 
99 @safe pure @nogc nothrow
100 bool isNumber(string str)
101 {
102 	if (str.length < 1)
103 		return false;
104 	if (str[0] == '-' || str[0] == '+')
105 		str = str[1 .. $];
106 	foreach (ch; str)
107 		if (ch < '0' || ch > '9')
108 			return false;
109 
110 	return true;
111 }
112 
113 ///
114 @safe pure
115 @("String: isNumber")
116 unittest
117 {
118 	assertTrue(isNumber("1"));
119 	assertTrue(isNumber("-3"));
120 	assertTrue(isNumber("+2"));
121 	assertTrue(isNumber("043"));
122 	assertFalse(isNumber("p3"));
123 }
124 
125 @safe pure @nogc nothrow
126 ptrdiff_t indexOfAny(string str, in char[] chars)
127 {
128 	ptrdiff_t ret = -1;
129 	foreach (ch; chars)
130 	{
131 		auto idx = str.indexOf(ch);
132 		if (idx >= 0 && (ret < 0 || idx < ret))
133 			ret = idx;
134 	}
135 	return ret;
136 }
137 
138 ///
139 @safe pure
140 @("String: indexOfAny")
141 unittest
142 {
143 	assertEquals(0, indexOfAny("-+", ['-', '+']));
144 	assertEquals(2, indexOfAny("11a", ['a']));
145 	assertEquals(-1, indexOfAny("11", ['a']));
146 }