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 version (Ares) 00042 private import std.c.ctype; 00043 else 00044 private import std.ctype; 00045 00046 00047 /****************************************************************************** 00048 00049 ******************************************************************************/ 00050 00051 extern (C) 00052 { 00053 char* memchr (char *, char, uint); 00054 int memcmp (char *, char *, uint); 00055 } 00056 00057 /****************************************************************************** 00058 00059 Placeholder for a variety of wee functions. Some of these are 00060 handy for Java programmers, but the primary reason for their 00061 existance is that they don't allocate memory (unlike Phobos). 00062 That is, processing is performed in-place or within a client- 00063 provided temporary buffer. 00064 00065 ******************************************************************************/ 00066 00067 abstract class Text 00068 { 00069 /********************************************************************** 00070 00071 Replace all instances of one char with another (in place) 00072 00073 **********************************************************************/ 00074 00075 final static char[] replace (char[] source, char match, char replacement) 00076 { 00077 char *p; 00078 char *scan = &source[0]; 00079 int length = source.length; 00080 00081 while ((p = memchr (scan, match, length)) != null) 00082 { 00083 *p = replacement; 00084 length -= (p - scan); 00085 scan = p; 00086 } 00087 return source; 00088 } 00089 00090 /********************************************************************** 00091 00092 Return the index of the first instance of 'match' 00093 00094 **********************************************************************/ 00095 00096 final static int indexOf (char[] source, char match) 00097 { 00098 return indexOf (source, match, 0); 00099 } 00100 00101 /********************************************************************** 00102 00103 Return the index of the first instance of 'match', starting 00104 at position 'start' 00105 00106 **********************************************************************/ 00107 00108 final static int indexOf (char[] source, char match, int start) 00109 { 00110 if (start < source.length) 00111 { 00112 char *p = memchr (&source[start], match, source.length - start); 00113 if (p) 00114 return start + (p - &source[start]); 00115 } 00116 return -1; 00117 } 00118 00119 /********************************************************************** 00120 00121 Return the index of the first instance of 'match' 00122 00123 **********************************************************************/ 00124 00125 final static int indexOf (char[] source, char[] match) 00126 { 00127 return indexOf (source, match, 0); 00128 } 00129 00130 /********************************************************************** 00131 00132 Return the index of the first instance of 'match', starting 00133 at position 'start' 00134 00135 **********************************************************************/ 00136 00137 final static int indexOf (char[] source, char[] match, int start) 00138 { 00139 int length = match.length; 00140 int extent = source.length - length + 1; 00141 00142 for (; start < extent; ++start) 00143 { 00144 start = indexOf (source, match[0], start); 00145 if (start < 0) 00146 break; 00147 else 00148 if (memcmp (&source[start], match, length) == 0) 00149 return start; 00150 } 00151 return -1; 00152 } 00153 00154 /********************************************************************** 00155 00156 Trim the provided string by stripping whitespace from 00157 both ends. Returns a slice of the original content. 00158 00159 **********************************************************************/ 00160 00161 final static char[] trim (char[] source) 00162 { 00163 if (source.length) 00164 { 00165 int front, 00166 back = source.length; 00167 00168 while (front < back && isspace(source[front])) 00169 ++front; 00170 00171 while (back > front && isspace(source[back-1])) 00172 --back; 00173 00174 if (front > 0 || back < source.length) 00175 return source[front..back]; 00176 } 00177 return source; 00178 } 00179 00180 /********************************************************************** 00181 00182 00183 **********************************************************************/ 00184 00185 final static char[][] split (char[] src, char[] delim) 00186 { 00187 int pos, 00188 mark; 00189 char[][] ret; 00190 00191 assert (delim.length); 00192 while ((pos = indexOf (src, delim, pos)) >= 0) 00193 { 00194 ret ~= src [mark..pos]; 00195 pos += delim.length; 00196 mark = pos; 00197 } 00198 00199 if (mark < src.length) 00200 ret ~= src [mark..src.length]; 00201 return ret; 00202 } 00203 00204 /********************************************************************** 00205 00206 in-place conversion to lowercase 00207 00208 **********************************************************************/ 00209 00210 final static char[] tolower (inout char[] src) 00211 { 00212 foreach (int i, char c; src) 00213 if (c >= 'A' && c <= 'Z') 00214 src[i] = c + ('a' - 'A'); 00215 return src; 00216 } 00217 } 00218