1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/**
* Yudit Unicode Editor Source File
*
* GNU Copyright (C) 1997-2023 Gaspar Sinai <gaspar@yudit.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* dated June 1991. See file COPYYING for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**
* @author: Gaspar Sinai <gaspar@yudit.org>
* @version: 2000-04-23
*/
#include "SStringVector.h"
#include <string.h>
SStringVector::SStringVector (void) : SVector<SString>()
{
}
SStringVector::SStringVector (const SStringVector& base) : SVector<SString>(base)
{
}
/**
* Do a split on base using delim.
* empty elements are not put in the resulting vector
*/
SStringVector::SStringVector (const SString& base, const SString& delim, bool once)
: SVector<SString>()
{
split(base, delim, once);
}
SStringVector::SStringVector (const SString& base)
: SVector<SString>()
{
split(base, ",");
}
SStringVector::SStringVector (const char* base)
: SVector<SString>()
{
split(base, ",");
}
SObject*
SStringVector::clone() const
{
return new SStringVector (*this);
}
SStringVector::~SStringVector ()
{
}
/**
* split a string into an array
* @param s is the string to split
* @param delim is the delimiter list
* @param once is true if we quit after one spli
* retgurn the number of splits
*/
unsigned int
SStringVector::split (const SString& s, const SString& delim, bool once)
{
derefer ();
char* excl = new char[256];
memset (excl, 0, 256);
unsigned int dsize = delim.size();
for (unsigned int d=0; d<dsize; d++)
{
unsigned char index = (unsigned char) delim.array()[d];
excl[index] = 1;
}
/* Find the first delim. */
unsigned int start = 0;
unsigned int count =0;
unsigned int ssize = s.size();
while (start < ssize)
{
unsigned int current;
for (current=start; current<s.size(); current++)
{
if (excl[(unsigned char)s.array()[current]] == 1) break;
}
if (current != start)
{
SString rep (s.array(), start, current-start);
append (rep);
count++;
if (once)
{
if (current < s.size())
{
SString ns (s.array(), current+1, s.size()-current-1);
append (ns);
count++;
}
return count;
}
}
start = (current+1);
}
delete []excl;
return count;
}
/**
* Remove delimiters
* return the index which matched.
*/
int
SStringVector::trim(SString *s) const
{
int found =-1;
unsigned int i;
int less = 0;
int where = -1;
for (i=0; i<size(); i++)
{
found = s->find (*peek(i));
if (found<0) continue;
if (found < less || where ==-1)
{
less = found;
where = i;
}
}
if (where >= 0)
{
s->truncate (less);
return where;
}
return -1;
}
SString
SStringVector::join(const SString& delimiter) const
{
SString str;
for (unsigned int i=0; i<size(); i++)
{
str.append (*peek(i));
if (size() != i+1)
{
str.append (delimiter);
}
}
return SString(str);
}
unsigned int
SStringVector::smartSplit (const SString& sin)
{
derefer ();
char quoted = 0;
char* excl = new char[256];
memset (excl, 0, 256);
SString delim("\t\n\r ");
SString s (sin);
for (unsigned int d=0; d<delim.size(); d++)
{
unsigned char index = (unsigned char) delim.array()[d];
excl[index] = 1;
}
/* Find the first delim. */
unsigned int start = 0;
unsigned int count =0;
bool escaped = false;
while (start < s.size())
{
unsigned int current;
escaped = false;
for (current=start; current<s.size(); current++)
{
if (quoted==0)
{
if (!escaped && (s.array()[current] == '\'' || s.array()[current] == '\"'))
{
quoted = s.array()[current];
start++;
continue;
}
if (s.array()[current] == '\\' && current+1<s.size())
{
s.remove (current);
escaped = true;
continue;
}
escaped = false;
if (excl[(unsigned char)s.array()[current]] == 1)
{
break;
}
}
else
{
if (s.array()[current] == '\\' && current+1<s.size())
{
s.remove (current);
escaped = true;
continue;
}
if (!escaped && s.array()[current] == quoted)
{
break;
}
escaped = false;
}
}
if (current != start || quoted)
{
if (current == start)
{
append (SString(""));
}
else
{
SString rep (s.array(), start, current-start);
char c = rep[rep.size()-1];
if (quoted && (c == '\'' || c == '"'))
{
rep.truncate (rep.size()-1);
}
append (rep);
}
quoted = 0;
count++;
}
start = (current+1);
}
delete []excl;
return count;
}
void
SStringVector::sort(bool ascending)
{
if (size()<2) return;
unsigned int* indeces = new unsigned int[size()];
CHECK_NEW(indeces);
unsigned int i;
for (i=0; i<size(); i++)
{
indeces[i] = i;
}
sort (indeces, ascending, 0, size()-1);
SStringVector nv(*this);
clear();
for (i=0; i<nv.size(); i++)
{
//fprintf (stderr, "append[%u]=%u [%*.*s]\n", i,
// indeces[i], SSARGS(nv[indeces[i]]));
append (nv[indeces[i]]);
}
delete[] indeces;
return;
}
void
SStringVector::sort(unsigned int* indeces, bool ascending,
int left, int right)
{
int i=left;
int j=right;
int pivot;
int mid;
int asc = (ascending)?-1:1;
if (right > left)
{
pivot = (right+left)/2;
mid= pivot;
// Make a partition
while (true)
{
// We compare everything to the pivot
while (i<right && (compare (indeces, i, mid) * asc) >= 0) i++;
while (j>left && (compare (indeces, j, mid) * asc) < 0) j--;
// Left index reached right index
if (i>=j) break;
// swap - it may swap the pivot as well.
if (i==mid) mid=j;
else if (j==mid) mid =i;
// Swap
unsigned int o = indeces[(unsigned int)i];
indeces[(unsigned int)i] = indeces[(unsigned int)j];
indeces[(unsigned int)j] = o;
}
// Make sure we don't sort the pivot - originally it is
// in the left array so we exchange it with something
// that belongs to left and lies in the boundary.
// It might be the pivot itself...
unsigned int o = indeces[(unsigned int)mid];
indeces[(unsigned int)mid] = indeces[(unsigned int)j];
indeces[(unsigned int)j] = o;
sort (indeces, ascending, left, j-1);
sort (indeces, ascending, i, right);
}
}
/**
* compare addreseed through indeces.
*/
int
SStringVector::compare (unsigned int* indeces, int i1, int i2)
{
const SString& js = *peek (indeces[(unsigned int) i1]);
const SString& ms = *peek (indeces[(unsigned int) i2]);
return js.compare(ms);
}
void
SStringVector::append (const SStringVector& v)
{
unsigned int sz = v.size();
for (unsigned int i=0; i<sz; i++)
{
append (v[i]);
}
}
void
SStringVector::append (const SString& str)
{
SVector<SString>::append (str);
}
void
SStringVector::append (const char* str)
{
SVector<SString>::append (SString(str));
}
|