Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

Text.d

Go to the documentation of this file.
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.date,
00042                 std.ctype;
00043 
00044 private import  std.c.stdio;
00045 
00046 /******************************************************************************
00047 
00048 ******************************************************************************/
00049 
00050 extern (C)
00051 {
00052         char* memchr (char *, char, uint);
00053         int   memcmp (char *, char *, uint);
00054 }       
00055 
00056 /******************************************************************************
00057 
00058         Placeholder for a variety of wee functions. Some of these are
00059         handy for Java programmers, but the primary reason for their
00060         existance is that they don't allocate memory (unlike Phobos).
00061         That is, processing is performed in-place or within a client-
00062         provided temporary buffer.
00063 
00064 ******************************************************************************/
00065 
00066 class Text
00067 {
00068         /**********************************************************************
00069 
00070                 Replace all instances of one char with another (in place)
00071 
00072         **********************************************************************/
00073 
00074         final static char[] replace (char[] source, char match, char replacement)
00075         {
00076                 char *p;
00077                 char *scan = &source[0];
00078                 int length = source.length;
00079 
00080                 while ((p = memchr (scan, match, length)) != null)
00081                       {
00082                       *p = replacement;
00083                       length -= (p - scan);
00084                       scan = p;
00085                       }
00086                 return source;
00087         }
00088 
00089         /**********************************************************************
00090 
00091                 Return the index of the first instance of 'match'
00092 
00093         **********************************************************************/
00094 
00095         final static int indexOf (char[] source, char match)
00096         {
00097                 return indexOf (source, match, 0);
00098         }
00099 
00100         /**********************************************************************
00101 
00102                 Return the index of the first instance of 'match', starting
00103                 at position 'start'
00104                 
00105         **********************************************************************/
00106 
00107         final static int indexOf (char[] source, char match, int start)
00108         {
00109                 if (start < source.length)
00110                    {
00111                    char *p = memchr (&source[start], match, source.length - start);
00112                    if (p)
00113                        return start + (p - &source[start]);
00114                    }
00115                 return -1;
00116         }
00117 
00118         /**********************************************************************
00119 
00120                 Return the index of the first instance of 'match'
00121 
00122         **********************************************************************/
00123 
00124         final static int indexOf (char[] source, char[] match)
00125         {
00126                 return indexOf (source, match, 0);
00127         }
00128 
00129         /**********************************************************************
00130 
00131                 Return the index of the first instance of 'match', starting
00132                 at position 'start'
00133                 
00134         **********************************************************************/
00135 
00136         final static int indexOf (char[] source, char[] match, int start)
00137         {
00138                 int length = match.length;
00139                 int extent = source.length - length + 1;
00140                 
00141                 for (; start < extent; ++start)
00142                     {
00143                     start = indexOf (source, match[0], start);
00144                     if (start < 0)
00145                         break;
00146                     else
00147                        if (memcmp (&source[start], match, length) == 0)
00148                            return start;
00149                     }
00150                 return -1;
00151         }
00152 
00153         /**********************************************************************
00154 
00155                 Trim the provided string by stripping whitespace from 
00156                 both ends. Returns a slice of the original content.
00157 
00158         **********************************************************************/
00159 
00160         final static char[] trim (char[] source)
00161         {
00162                 if (source.length)
00163                    {
00164                    int  front,
00165                         back = source.length;
00166 
00167                    while (front < back && isspace(source[front]))
00168                           ++front;
00169 
00170                    while (back > front && isspace(source[back-1]))
00171                           --back;
00172                    
00173                    if (front > 0 || back < source.length)   
00174                        return source[front..back];
00175                    } 
00176                 return source;
00177         }
00178 
00179         /**********************************************************************
00180 
00181                 Convert a string into a base-10 number.
00182 
00183         **********************************************************************/
00184 
00185 
00186         final static int atoi (char[] digits)
00187         {
00188                 return atoi (digits, 10);
00189         }
00190 
00191         /**********************************************************************
00192 
00193                 Convert a string into a base-X number where X is specified
00194                 by 'radix'
00195 
00196         **********************************************************************/
00197 
00198         final static int atoi (char[] digits, int radix)
00199         {
00200                 int i;
00201 
00202                 foreach (char c; digits)
00203                         {
00204                         if (isspace(c))
00205                             continue;
00206 
00207                         if (c >= 'a' && c <= 'f')
00208                             c -= 39;
00209                         else
00210                            if (c >= 'A' && c <= 'F')
00211                                c -= 7;
00212                            else
00213                               if (c < '0' || c > '9')
00214                                   throw new Exception ("invalid number '"~digits~"'");
00215 
00216                         i = i * radix + c - '0';
00217                         }
00218                 return i;
00219         }
00220 
00221 
00222         /**********************************************************************
00223 
00224                 Convert an integer to a string, without allocating space.
00225 
00226         **********************************************************************/
00227 
00228         final static char[] itoa (char[] s, long i)
00229         in {
00230            assert (s.length > 0);
00231            }
00232         body 
00233         {
00234                 int len = s.length;
00235                 do {
00236                    char c = i % 10 + '0';
00237                    s[--len] = c;
00238                    i /= 10;
00239                    } while (i && len);
00240                 return s[len..s.length];
00241         }
00242 
00243         /**********************************************************************
00244 
00245                 Convert an integer to a string
00246 
00247         **********************************************************************/
00248 
00249         final static char[] toString (int i)
00250         {
00251                 char t[10];
00252 
00253                 return itoa (t, i).dup;
00254         }
00255 
00256         /**********************************************************************
00257 
00258                 Convert a string to a date. This is just a symmetrical 
00259                 wrapper around the Phobos functionality.
00260                
00261         **********************************************************************/
00262 
00263         final static long atod (char[] date)
00264         {
00265                 return parse (date);
00266         }
00267 
00268         /**********************************************************************
00269 
00270                 Shamelessly plundered from std.date (with permission), this 
00271                 version avoids allocating memory.
00272 
00273                 Returns a populated slice of the provided buffer; with zero
00274                 length if the date was invalid.
00275 
00276         **********************************************************************/
00277 
00278         final static char[] dtoa (char[] buffer, long time)
00279         {
00280                 int     hr;
00281                 int     mn;
00282                 int     len;
00283                 char    sign;
00284                 d_time  t,
00285                         dst,
00286                         offset;
00287 
00288                 // Years are supposed to be -285616 .. 285616, or 7 digits
00289                 // "Tue Apr 02 02:04:57 GMT-0800 1996"
00290                 assert(buffer.length >= 29 + 7 + 1);
00291 
00292                 if (time == d_time_nan)
00293                     return buffer[0..0];
00294 
00295                 dst = DaylightSavingTA (time);
00296                 offset = LocalTZA + dst;
00297                 t = time + offset;
00298                 sign = '+';
00299                 if (offset < 0)
00300                    {
00301                    sign = '-';
00302                    offset = -(LocalTZA + dst);
00303                    }
00304 
00305                 mn = cast(int)(offset / msPerMinute);
00306                 hr = mn / 60;
00307                 mn %= 60;
00308 
00309                 len = sprintf (buffer, "%.3s %.3s %02d %02d:%02d:%02d GMT%c%02d%02d %d",
00310                                &daystr[WeekDay(t) * 3],
00311                                &monstr[MonthFromTime(t) * 3],
00312                                DateFromTime(t),
00313                                cast(int)HourFromTime(t), cast(int)MinFromTime(t), 
00314                                cast(int)SecFromTime(t), sign, hr, mn,
00315                                cast(long)YearFromTime(t));
00316 
00317                 return buffer[0..len];
00318         }
00319 
00320         /**********************************************************************
00321 
00322                 in-place conversion to lowercase 
00323 
00324         **********************************************************************/
00325 
00326         final static char[] tolower (inout char[] src)
00327         {
00328                 foreach (int i, char c; src)
00329                          if (c >= 'A' && c <= 'Z')
00330                              src[i] = c + ('a' - 'A');
00331                 return src;
00332         }
00333 
00334 }
00335 

Generated on Tue Jan 25 21:18:23 2005 for Mango by doxygen 1.3.6