col.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00038 #include "fastlib/col/arraylist.h"
00039 #include "fastlib/col/col_string.h"
00040
00041
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
00062 size = len + 1;
00063 } else {
00064
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
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 }