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 
00027                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00028 
00029         
00030         @version        Initial version, April 2004      
00031         @author         Kris
00032 
00033 
00034 *******************************************************************************/
00035 
00036 module mango.utils.Text;
00037 
00038 private import  std.date,
00039                 std.ctype;
00040 
00041 private import  std.c.stdio;
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                 Convert a string into a base-10 number.
00179 
00180         **********************************************************************/
00181 
00182 
00183         final static int atoi (char[] digits)
00184         {
00185                 return atoi (digits, 10);
00186         }
00187 
00188         /**********************************************************************
00189 
00190                 Convert a string into a base-X number where X is specified
00191                 by 'radix'
00192 
00193         **********************************************************************/
00194 
00195         final static int atoi (char[] digits, int radix)
00196         {
00197                 int i;
00198 
00199                 foreach (char c; digits)
00200                         {
00201                         if (isspace(c))
00202                             continue;
00203 
00204                         if (c >= 'a' && c <= 'f')
00205                             c -= 39;
00206                         else
00207                            if (c >= 'A' && c <= 'F')
00208                                c -= 7;
00209                            else
00210                               if (c < '0' || c > '9')
00211                                   throw new Exception ("invalid number '"~digits~"'");
00212 
00213                         i = i * radix + c - '0';
00214                         }
00215                 return i;
00216         }
00217 
00218 
00219         /**********************************************************************
00220 
00221                 Convert an integer to a string, without allocating space.
00222 
00223         **********************************************************************/
00224 
00225         final static char[] itoa (char[] s, long i)
00226         in {
00227            assert (s.length > 0);
00228            }
00229         body 
00230         {
00231                 int len = s.length;
00232                 do {
00233                    char c = i % 10 + '0';
00234                    s[--len] = c;
00235                    i /= 10;
00236                    } while (i && len);
00237                 return s[len..s.length];
00238         }
00239 
00240         /**********************************************************************
00241 
00242                 Convert an integer to a string
00243 
00244         **********************************************************************/
00245 
00246         final static char[] toString (int i)
00247         {
00248                 char t[10];
00249 
00250                 return itoa (t, i).dup;
00251         }
00252 
00253         /**********************************************************************
00254 
00255                 Convert a string to a date. This is just a symmetrical 
00256                 wrapper around the Phobos functionality.
00257                
00258         **********************************************************************/
00259 
00260         final static long atod (char[] date)
00261         {
00262                 return parse (date);
00263         }
00264 
00265         /**********************************************************************
00266 
00267                 Shamelessly plundered from std.date (with permission), this 
00268                 version avoids allocating memory.
00269 
00270                 Returns a populated slice of the provided buffer; with zero
00271                 length if the date was invalid.
00272 
00273         **********************************************************************/
00274 
00275         final static char[] dtoa (char[] buffer, long time)
00276         {
00277                 int     hr;
00278                 int     mn;
00279                 int     len;
00280                 char    sign;
00281                 d_time  t,
00282                         dst,
00283                         offset;
00284 
00285                 // Years are supposed to be -285616 .. 285616, or 7 digits
00286                 // "Tue Apr 02 02:04:57 GMT-0800 1996"
00287                 assert(buffer.length >= 29 + 7 + 1);
00288 
00289                 if (time == d_time_nan)
00290                     return buffer[0..0];
00291 
00292                 dst = DaylightSavingTA (time);
00293                 offset = LocalTZA + dst;
00294                 t = time + offset;
00295                 sign = '+';
00296                 if (offset < 0)
00297                    {
00298                    sign = '-';
00299                    offset = -(LocalTZA + dst);
00300                    }
00301 
00302                 mn = cast(int)(offset / msPerMinute);
00303                 hr = mn / 60;
00304                 mn %= 60;
00305 
00306                 len = sprintf (buffer, "%.3s %.3s %02d %02d:%02d:%02d GMT%c%02d%02d %d",
00307                                &daystr[WeekDay(t) * 3],
00308                                &monstr[MonthFromTime(t) * 3],
00309                                DateFromTime(t),
00310                                cast(int)HourFromTime(t), cast(int)MinFromTime(t), 
00311                                cast(int)SecFromTime(t), sign, hr, mn,
00312                                cast(long)YearFromTime(t));
00313 
00314                 return buffer[0..len];
00315         }
00316 
00317         /**********************************************************************
00318 
00319                 in-place conversion to lowercase 
00320 
00321         **********************************************************************/
00322 
00323         final static char[] tolower (inout char[] src)
00324         {
00325                 foreach (int i, char c; src)
00326                          if (c >= 'A' && c <= 'Z')
00327                              src[i] = c + ('a' - 'A');
00328                 return src;
00329         }
00330 
00331 }
00332 

Generated on Sun Nov 7 19:06:53 2004 for Mango by doxygen 1.3.6