col.cc

Go to the documentation of this file.
00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00038 #include "fastlib/col/arraylist.h"
00039 #include "fastlib/col/col_string.h"
00040 //#include "arraylist.h"
00041 //#include "col_string.h"
00042 
00043 #include <stdarg.h>
00044 
00045 const String& String::InitSprintf(const char *format, ...) {
00046   int size = 128;
00047   int len;
00048   char *s = mem::Alloc<char>(size);
00049   va_list vl;
00050   
00051   while (1) {
00052     va_start(vl, format);
00053     len = vsnprintf(s, size, format, vl);
00054     va_end(vl);
00055     
00056     if (likely(len > -1) && likely(len < size)) {
00057       break;
00058     }
00059     
00060     if (len > -1) {
00061       // snprintf is telling us exactly how long it should be
00062       size = len + 1;
00063     } else {
00064       // snprintf gave up and said it's too small, let's try again
00065       size *= 2;
00066     }
00067     
00068     s = mem::Realloc(s, size);
00069   }
00070   
00071   Steal(s, len, size);
00072   
00073   return *this;
00074 }
00075 
00076 index_t String::FindAny(const char *char_set, index_t skip_initial) const {
00077   const char *pos = begin() + skip_initial;
00078   
00079   DEBUG_BOUNDS(skip_initial, length() + 1);
00080   
00081   while (*pos != '\0' && strchr(char_set, *pos) == NULL) {
00082     pos++;
00083   }
00084   
00085   if (*pos == '\0') {
00086     return -1;
00087   } else {
00088     return pos - begin();
00089   }
00090 }
00091 
00092 index_t String::Split(index_t start_index,
00093     const char *delimeters,
00094     const char *donechars,
00095     index_t max_portions,
00096     ArrayList<String> *result) const {
00097   const char *pos = begin() + start_index;
00098   bool done = false;
00099   
00100   DEBUG_BOUNDS(start_index, length() + 1);
00101   
00102   do {
00103     const char *startpos;
00104     const char *endpos;
00105 
00106     while (*pos != '\0' && strchr(delimeters, *pos) != NULL) {
00107       pos++;
00108     }
00109     
00110     startpos = endpos = pos;
00111     
00112     while (1) {
00113       if (unlikely(*endpos == '\0') || strchr(donechars, *endpos) != NULL) {
00114         // strip extra delimeters from right side
00115         while (endpos > startpos && strchr(delimeters, endpos[-1]) != NULL) {
00116           endpos--;
00117         }
00118         done = true;
00119         break;
00120       }
00121       if (max_portions != 1 && strchr(delimeters, *endpos) != NULL) {
00122         break;
00123       }
00124       endpos++;
00125     }
00126     
00127     pos = endpos;
00128     
00129     max_portions--;
00130     
00131     if (likely(startpos != endpos)) {
00132       result->PushBack();
00133       result->back().Copy(startpos, endpos - startpos);
00134     }
00135   } while (!done);
00136   
00137   return pos - begin();
00138 }
00139 
00140 void String::TrimLeft(const char *delimeters, String *result) const {
00141   const char *s = begin();
00142   while (*s != '\0' && strchr(delimeters, *s)) {
00143     s++;
00144   }
00145   result->Copy(s, end() - s);
00146 }
00147 
00148 void String::TrimRight(const char *delimeters, String *result) const {
00149   const char *s = end() - 1;
00150   const char *b = begin();
00151   while (s >= b && strchr(delimeters, *s)) {
00152     s--;
00153   }
00154   result->Copy(b, s - b + 1);
00155 }
00156 
00157 void String::Trim(const char *delimeters, String *result) const {
00158   const char *b = begin();
00159   const char *e = end() - 1;
00160   while (e >= b && strchr(delimeters, *e)) {
00161     e--;
00162   }
00163   while (e >= b && strchr(delimeters, *b)) {
00164     b++;
00165   }
00166   result->Copy(b, e - b + 1);
00167 }
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3