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 44 @nogc @safe pure nothrow 45 string substr(string s, ptrdiff_t offset, ptrdiff_t length = -1) 46 { 47 size_t end = void; 48 import std.stdio; 49 50 if (offset > 0 && offset > s.length) 51 return ""; 52 if (offset < 0) 53 offset = 0; 54 if (length < 0) 55 end = s.length; 56 else 57 end = offset + length; 58 if(end > s.length) 59 end = s.length; 60 61 return s[offset .. end]; 62 } 63 64 65 /// 66 @safe pure 67 @("String: substr") 68 unittest 69 { 70 string s = "Aurora Framework"; 71 assertEquals("urora", s.substr(1,5)); 72 assertEquals("urora Framework", s.substr(1)); 73 assertEquals(s, s.substr(-1, -1)); 74 assertEquals(s, s.substr(-1)); 75 assertEquals(s, s.substr(0, ptrdiff_t.max)); 76 assertEquals("", s.substr(ptrdiff_t.max)); 77 } 78 79 80 @nogc @safe pure nothrow 81 bool isAlpha(string str) 82 { 83 import std.algorithm : all; 84 import std.uni : isAlpha; 85 86 foreach(c; str) 87 if (!isAlpha(c)) 88 return false; 89 90 return true; 91 } 92 93 94 /// 95 @safe pure 96 @("String: isAlpha") 97 unittest 98 { 99 assertTrue(isAlpha("tunaisgood")); 100 assertFalse(isAlpha("tun41s900d")); 101 } 102 103 104 @safe pure @nogc nothrow 105 bool isNumber(string str) 106 { 107 if (str.length < 1) return false; 108 if(str[0] == '-' || str[0] == '+') 109 str = str[1 .. $]; 110 foreach (ch; str) 111 if (ch < '0' || ch > '9') 112 return false; 113 114 return true; 115 } 116 117 118 /// 119 @safe pure 120 @("String: isNumber") 121 unittest 122 { 123 assertTrue(isNumber("1")); 124 assertTrue(isNumber("-3")); 125 assertTrue(isNumber("+2")); 126 assertTrue(isNumber("043")); 127 assertFalse(isNumber("p3")); 128 } 129 130 131 @safe pure @nogc nothrow 132 ptrdiff_t indexOfAny(string str, in char[] chars) 133 { 134 ptrdiff_t ret = -1; 135 foreach (ch; chars) { 136 auto idx = str.indexOf(ch); 137 if (idx >= 0 && (ret < 0 || idx < ret)) 138 ret = idx; 139 } 140 return ret; 141 } 142 143 144 /// 145 @safe pure 146 @("String: indexOfAny") 147 unittest 148 { 149 assertEquals(0, indexOfAny("-+", ['-', '+'])); 150 assertEquals(2, indexOfAny("11a", ['a'])); 151 assertEquals(-1, indexOfAny("11", ['a'])); 152 }