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.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                 in-place conversion to lowercase 
00179 
00180         **********************************************************************/
00181 
00182         final static char[] tolower (inout char[] src)
00183         {
00184                 foreach (int i, char c; src)
00185                          if (c >= 'A' && c <= 'Z')
00186                              src[i] = c + ('a' - 'A');
00187                 return src;
00188         }
00189 }
00190 

Generated on Sun Mar 6 00:30:59 2005 for Mango by doxygen 1.3.6