00001 /******************************************************************************* 00002 00003 @file Text.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, April 2004 00034 @author Kris 00035 00036 00037 *******************************************************************************/ 00038 00039 module mango.utils.Text; 00040 00041 private import std.ctype; 00042 00043 /****************************************************************************** 00044 00045 ******************************************************************************/ 00046 00047 extern (C) 00048 { 00049 char* memchr (char *, char, uint); 00050 int memcmp (char *, char *, uint); 00051 } 00052 00053 /****************************************************************************** 00054 00055 Placeholder for a variety of wee functions. Some of these are 00056 handy for Java programmers, but the primary reason for their 00057 existance is that they don't allocate memory (unlike Phobos). 00058 That is, processing is performed in-place or within a client- 00059 provided temporary buffer. 00060 00061 ******************************************************************************/ 00062 00063 class Text 00064 { 00065 /********************************************************************** 00066 00067 Replace all instances of one char with another (in place) 00068 00069 **********************************************************************/ 00070 00071 final static char[] replace (char[] source, char match, char replacement) 00072 { 00073 char *p; 00074 char *scan = &source[0]; 00075 int length = source.length; 00076 00077 while ((p = memchr (scan, match, length)) != null) 00078 { 00079 *p = replacement; 00080 length -= (p - scan); 00081 scan = p; 00082 } 00083 return source; 00084 } 00085 00086 /********************************************************************** 00087 00088 Return the index of the first instance of 'match' 00089 00090 **********************************************************************/ 00091 00092 final static int indexOf (char[] source, char match) 00093 { 00094 return indexOf (source, match, 0); 00095 } 00096 00097 /********************************************************************** 00098 00099 Return the index of the first instance of 'match', starting 00100 at position 'start' 00101 00102 **********************************************************************/ 00103 00104 final static int indexOf (char[] source, char match, int start) 00105 { 00106 if (start < source.length) 00107 { 00108 char *p = memchr (&source[start], match, source.length - start); 00109 if (p) 00110 return start + (p - &source[start]); 00111 } 00112 return -1; 00113 } 00114 00115 /********************************************************************** 00116 00117 Return the index of the first instance of 'match' 00118 00119 **********************************************************************/ 00120 00121 final static int indexOf (char[] source, char[] match) 00122 { 00123 return indexOf (source, match, 0); 00124 } 00125 00126 /********************************************************************** 00127 00128 Return the index of the first instance of 'match', starting 00129 at position 'start' 00130 00131 **********************************************************************/ 00132 00133 final static int indexOf (char[] source, char[] match, int start) 00134 { 00135 int length = match.length; 00136 int extent = source.length - length + 1; 00137 00138 for (; start < extent; ++start) 00139 { 00140 start = indexOf (source, match[0], start); 00141 if (start < 0) 00142 break; 00143 else 00144 if (memcmp (&source[start], match, length) == 0) 00145 return start; 00146 } 00147 return -1; 00148 } 00149 00150 /********************************************************************** 00151 00152 Trim the provided string by stripping whitespace from 00153 both ends. Returns a slice of the original content. 00154 00155 **********************************************************************/ 00156 00157 final static char[] trim (char[] source) 00158 { 00159 if (source.length) 00160 { 00161 int front, 00162 back = source.length; 00163 00164 while (front < back && isspace(source[front])) 00165 ++front; 00166 00167 while (back > front && isspace(source[back-1])) 00168 --back; 00169 00170 if (front > 0 || back < source.length) 00171 return source[front..back]; 00172 } 00173 return source; 00174 } 00175 00176 /********************************************************************** 00177 00178 00179 **********************************************************************/ 00180 00181 final static char[][] split (char[] src, char[] delim) 00182 { 00183 int pos, 00184 mark; 00185 char[][] ret; 00186 00187 assert (delim.length); 00188 while ((pos = indexOf (src, delim, pos)) >= 0) 00189 { 00190 ret ~= src [mark..pos]; 00191 pos += delim.length; 00192 mark = pos; 00193 } 00194 return ret; 00195 } 00196 00197 /********************************************************************** 00198 00199 in-place conversion to lowercase 00200 00201 **********************************************************************/ 00202 00203 final static char[] tolower (inout char[] src) 00204 { 00205 foreach (int i, char c; src) 00206 if (c >= 'A' && c <= 'Z') 00207 src[i] = c + ('a' - 'A'); 00208 return src; 00209 } 00210 } 00211