1*7a4715d3SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*7a4715d3SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*7a4715d3SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*7a4715d3SAndrew Rist * distributed with this work for additional information 6*7a4715d3SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*7a4715d3SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*7a4715d3SAndrew Rist * "License"); you may not use this file except in compliance 9*7a4715d3SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*7a4715d3SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*7a4715d3SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*7a4715d3SAndrew Rist * software distributed under the License is distributed on an 15*7a4715d3SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*7a4715d3SAndrew Rist * KIND, either express or implied. See the License for the 17*7a4715d3SAndrew Rist * specific language governing permissions and limitations 18*7a4715d3SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*7a4715d3SAndrew Rist *************************************************************/ 21*7a4715d3SAndrew Rist 22*7a4715d3SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_soltools.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir 28cdf0e10cSrcweir #include <simstr.hxx> 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include <string.h> // strlen(), memcpy(), memset() 31cdf0e10cSrcweir #include <ctype.h> // tolower() 32cdf0e10cSrcweir #include <limits.h> // INT_MAX 33cdf0e10cSrcweir 34cdf0e10cSrcweir const char NULCH = '\0'; 35cdf0e10cSrcweir const int NO_POS = -1; 36cdf0e10cSrcweir 37cdf0e10cSrcweir 38cdf0e10cSrcweir Simstr::Simstr(const char * s_) 39cdf0e10cSrcweir { 40cdf0e10cSrcweir if (s_ == 0) 41cdf0e10cSrcweir { 42cdf0e10cSrcweir len = 0; 43cdf0e10cSrcweir sz = new char[1]; 44cdf0e10cSrcweir *sz = 0; 45cdf0e10cSrcweir } 46cdf0e10cSrcweir else 47cdf0e10cSrcweir { 48cdf0e10cSrcweir len = strlen(s_); 49cdf0e10cSrcweir sz = new char[len+1]; 50cdf0e10cSrcweir memcpy(sz,s_,len+1); 51cdf0e10cSrcweir } 52cdf0e10cSrcweir } 53cdf0e10cSrcweir 54cdf0e10cSrcweir Simstr::Simstr(const char * anybytes, int nrOfBytes) 55cdf0e10cSrcweir { 56cdf0e10cSrcweir if (anybytes == 0) 57cdf0e10cSrcweir { 58cdf0e10cSrcweir len = 0; 59cdf0e10cSrcweir sz = new char[1]; 60cdf0e10cSrcweir *sz = 0; 61cdf0e10cSrcweir return; 62cdf0e10cSrcweir } 63cdf0e10cSrcweir 64cdf0e10cSrcweir int slen = static_cast<int>( strlen(anybytes) ); 65cdf0e10cSrcweir 66cdf0e10cSrcweir len = slen < nrOfBytes 67cdf0e10cSrcweir ? slen 68cdf0e10cSrcweir : nrOfBytes; 69cdf0e10cSrcweir sz = new char[len+1]; 70cdf0e10cSrcweir memcpy( sz, anybytes, len ); 71cdf0e10cSrcweir *( sz + len ) = 0; 72cdf0e10cSrcweir } 73cdf0e10cSrcweir 74cdf0e10cSrcweir Simstr::Simstr(char c, int anzahl) 75cdf0e10cSrcweir { 76cdf0e10cSrcweir if (anzahl < 1) 77cdf0e10cSrcweir { 78cdf0e10cSrcweir len = 0; 79cdf0e10cSrcweir sz = new char[1]; 80cdf0e10cSrcweir *sz = 0; 81cdf0e10cSrcweir } 82cdf0e10cSrcweir else 83cdf0e10cSrcweir { 84cdf0e10cSrcweir len = anzahl; 85cdf0e10cSrcweir sz = new char[len+1]; 86cdf0e10cSrcweir memset(sz,c,anzahl); 87cdf0e10cSrcweir sz[len] = 0; 88cdf0e10cSrcweir } 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir Simstr::Simstr( const char * anybytes, 92cdf0e10cSrcweir int firstBytesPos, 93cdf0e10cSrcweir int nrOfBytes) 94cdf0e10cSrcweir { 95cdf0e10cSrcweir unsigned slen = strlen(anybytes); 96cdf0e10cSrcweir if (anybytes == 0 || slen <= unsigned(firstBytesPos)) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir len = 0; 99cdf0e10cSrcweir sz = new char[1]; 100cdf0e10cSrcweir *sz = 0; 101cdf0e10cSrcweir } 102cdf0e10cSrcweir else 103cdf0e10cSrcweir { 104cdf0e10cSrcweir int maxLen = slen - unsigned(firstBytesPos); 105cdf0e10cSrcweir len = maxLen < nrOfBytes 106cdf0e10cSrcweir ? maxLen 107cdf0e10cSrcweir : nrOfBytes; 108cdf0e10cSrcweir sz = new char[len+1]; 109cdf0e10cSrcweir memcpy(sz,anybytes+firstBytesPos,len); 110cdf0e10cSrcweir *(sz+len) = 0; 111cdf0e10cSrcweir } 112cdf0e10cSrcweir } 113cdf0e10cSrcweir 114cdf0e10cSrcweir 115cdf0e10cSrcweir Simstr::Simstr(const Simstr & S) 116cdf0e10cSrcweir { 117cdf0e10cSrcweir len = S.len; 118cdf0e10cSrcweir sz = new char[len+1]; 119cdf0e10cSrcweir memcpy(sz,S.sz,len+1); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir 122cdf0e10cSrcweir Simstr & Simstr::operator=(const Simstr & S) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir if (sz == S.sz) 125cdf0e10cSrcweir return *this; 126cdf0e10cSrcweir 127cdf0e10cSrcweir delete [] sz; 128cdf0e10cSrcweir 129cdf0e10cSrcweir len = S.len; 130cdf0e10cSrcweir sz = new char[len+1]; 131cdf0e10cSrcweir memcpy(sz,S.sz,len+1); 132cdf0e10cSrcweir 133cdf0e10cSrcweir return *this; 134cdf0e10cSrcweir } 135cdf0e10cSrcweir 136cdf0e10cSrcweir Simstr::~Simstr() 137cdf0e10cSrcweir { 138cdf0e10cSrcweir delete [] sz; 139cdf0e10cSrcweir } 140cdf0e10cSrcweir 141cdf0e10cSrcweir char & 142cdf0e10cSrcweir Simstr::ch(int n) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir static char nullCh = NULCH; 145cdf0e10cSrcweir nullCh = NULCH; 146cdf0e10cSrcweir if (n >= long(len) || n < 0) 147cdf0e10cSrcweir return nullCh; 148cdf0e10cSrcweir else 149cdf0e10cSrcweir return sz[unsigned(n)]; 150cdf0e10cSrcweir } 151cdf0e10cSrcweir 152cdf0e10cSrcweir const Simstr & 153cdf0e10cSrcweir Simstr::null_() 154cdf0e10cSrcweir { 155cdf0e10cSrcweir static Simstr aNull_; 156cdf0e10cSrcweir return aNull_; 157cdf0e10cSrcweir } 158cdf0e10cSrcweir 159cdf0e10cSrcweir 160cdf0e10cSrcweir Simstr 161cdf0e10cSrcweir Simstr::operator+(const Simstr & S) const 162cdf0e10cSrcweir { 163cdf0e10cSrcweir Simstr ret = sz; 164cdf0e10cSrcweir ret.push_back(S); 165cdf0e10cSrcweir return ret; 166cdf0e10cSrcweir } 167cdf0e10cSrcweir 168cdf0e10cSrcweir Simstr & 169cdf0e10cSrcweir Simstr::operator+=(const Simstr & S) 170cdf0e10cSrcweir { 171cdf0e10cSrcweir push_back(S); 172cdf0e10cSrcweir return *this; 173cdf0e10cSrcweir } 174cdf0e10cSrcweir 175cdf0e10cSrcweir Simstr & 176cdf0e10cSrcweir Simstr::operator+=(const char * s_) 177cdf0e10cSrcweir { 178cdf0e10cSrcweir Simstr a(s_); 179cdf0e10cSrcweir push_back(a); 180cdf0e10cSrcweir return *this; 181cdf0e10cSrcweir } 182cdf0e10cSrcweir 183cdf0e10cSrcweir 184cdf0e10cSrcweir // REL 185cdf0e10cSrcweir 186cdf0e10cSrcweir bool 187cdf0e10cSrcweir Simstr::operator==(const Simstr & S) const 188cdf0e10cSrcweir { return !strcmp(sz,S.sz) ? true : false; } 189cdf0e10cSrcweir 190cdf0e10cSrcweir bool 191cdf0e10cSrcweir Simstr::operator!=(const Simstr & S) const 192cdf0e10cSrcweir { return strcmp(sz,S.sz) ? true : false; } 193cdf0e10cSrcweir 194cdf0e10cSrcweir bool 195cdf0e10cSrcweir Simstr::operator<(const Simstr & S) const 196cdf0e10cSrcweir { return (strcmp(sz,S.sz) < 0) ? true : false; } 197cdf0e10cSrcweir 198cdf0e10cSrcweir bool 199cdf0e10cSrcweir Simstr::operator>(const Simstr & S) const 200cdf0e10cSrcweir { return (strcmp(sz,S.sz) > 0) ? true : false; } 201cdf0e10cSrcweir 202cdf0e10cSrcweir bool 203cdf0e10cSrcweir Simstr::operator<=(const Simstr & S) const 204cdf0e10cSrcweir { return (strcmp(sz,S.sz) <= 0) ? true : false; } 205cdf0e10cSrcweir 206cdf0e10cSrcweir bool 207cdf0e10cSrcweir Simstr::operator>=(const Simstr & S) const 208cdf0e10cSrcweir { return (strcmp(sz,S.sz) >= 0) ? true : false; } 209cdf0e10cSrcweir 210cdf0e10cSrcweir 211cdf0e10cSrcweir 212cdf0e10cSrcweir 213cdf0e10cSrcweir // ************** LIST - Funktionen ***************** 214cdf0e10cSrcweir 215cdf0e10cSrcweir 216cdf0e10cSrcweir // Einzelzugriff 217cdf0e10cSrcweir 218cdf0e10cSrcweir char 219cdf0e10cSrcweir Simstr::get(int n) const { return (n >= len || n < 0) ? 0 : sz[n]; } 220cdf0e10cSrcweir 221cdf0e10cSrcweir char 222cdf0e10cSrcweir Simstr::get_front() const { return sz[0]; } 223cdf0e10cSrcweir 224cdf0e10cSrcweir char 225cdf0e10cSrcweir Simstr::get_back() const { return len ? sz[len-1] : 0; } 226cdf0e10cSrcweir 227cdf0e10cSrcweir Simstr 228cdf0e10cSrcweir Simstr::get(int startPos, int anzahl) const 229cdf0e10cSrcweir { 230cdf0e10cSrcweir if (startPos >= len || startPos < 0 || anzahl < 1) 231cdf0e10cSrcweir return ""; 232cdf0e10cSrcweir 233cdf0e10cSrcweir int anz = len - startPos < anzahl ? len - startPos : anzahl; 234cdf0e10cSrcweir 235cdf0e10cSrcweir Simstr ret(' ',anz); 236cdf0e10cSrcweir memcpy(ret.sz, sz+startPos, anz); 237cdf0e10cSrcweir return ret; 238cdf0e10cSrcweir } 239cdf0e10cSrcweir 240cdf0e10cSrcweir Simstr 241cdf0e10cSrcweir Simstr::get_front(int anzahl) const 242cdf0e10cSrcweir { 243cdf0e10cSrcweir int anz = len < anzahl ? len : anzahl; 244cdf0e10cSrcweir if (anz < 1) 245cdf0e10cSrcweir return ""; 246cdf0e10cSrcweir 247cdf0e10cSrcweir Simstr ret(' ',anz); 248cdf0e10cSrcweir memcpy(ret.sz, sz, anz); 249cdf0e10cSrcweir return ret; 250cdf0e10cSrcweir } 251cdf0e10cSrcweir 252cdf0e10cSrcweir Simstr 253cdf0e10cSrcweir Simstr::get_back(int anzahl) const 254cdf0e10cSrcweir { 255cdf0e10cSrcweir int anz = len < anzahl ? len : anzahl; 256cdf0e10cSrcweir if (anz < 1) 257cdf0e10cSrcweir return ""; 258cdf0e10cSrcweir int start = len-anz; 259cdf0e10cSrcweir 260cdf0e10cSrcweir Simstr ret(' ',anz); 261cdf0e10cSrcweir memcpy(ret.sz, sz+start, anz); 262cdf0e10cSrcweir return ret; 263cdf0e10cSrcweir } 264cdf0e10cSrcweir 265cdf0e10cSrcweir Simstr 266cdf0e10cSrcweir Simstr::get_first_token(char c) const 267cdf0e10cSrcweir { 268cdf0e10cSrcweir int posc = pos_first(c); 269cdf0e10cSrcweir if (posc != NO_POS) 270cdf0e10cSrcweir return get_front(posc); 271cdf0e10cSrcweir else 272cdf0e10cSrcweir return sz; 273cdf0e10cSrcweir } 274cdf0e10cSrcweir 275cdf0e10cSrcweir Simstr 276cdf0e10cSrcweir Simstr::get_last_token(char c) const 277cdf0e10cSrcweir { 278cdf0e10cSrcweir int posc = pos_last(c); 279cdf0e10cSrcweir if (posc != NO_POS) 280cdf0e10cSrcweir return get_back(len-posc-1); 281cdf0e10cSrcweir else 282cdf0e10cSrcweir return sz; 283cdf0e10cSrcweir } 284cdf0e10cSrcweir 285cdf0e10cSrcweir 286cdf0e10cSrcweir 287cdf0e10cSrcweir // Insert 288cdf0e10cSrcweir 289cdf0e10cSrcweir void 290cdf0e10cSrcweir Simstr::insert(int pos, char c) 291cdf0e10cSrcweir { 292cdf0e10cSrcweir if (pos < 0 || pos > len) 293cdf0e10cSrcweir return; 294cdf0e10cSrcweir 295cdf0e10cSrcweir char * result = new char[len+2]; 296cdf0e10cSrcweir 297cdf0e10cSrcweir memcpy(result,sz,pos); 298cdf0e10cSrcweir result[pos] = c; 299cdf0e10cSrcweir memcpy(result+pos+1,sz+pos,len-pos+1); 300cdf0e10cSrcweir 301cdf0e10cSrcweir delete [] sz; 302cdf0e10cSrcweir sz = result; 303cdf0e10cSrcweir len++; 304cdf0e10cSrcweir } 305cdf0e10cSrcweir 306cdf0e10cSrcweir void 307cdf0e10cSrcweir Simstr::push_front(char c) 308cdf0e10cSrcweir { 309cdf0e10cSrcweir char * result = new char[len+2]; 310cdf0e10cSrcweir 311cdf0e10cSrcweir result[0] = c; 312cdf0e10cSrcweir memcpy(result+1,sz,len+1); 313cdf0e10cSrcweir 314cdf0e10cSrcweir delete [] sz; 315cdf0e10cSrcweir sz = result; 316cdf0e10cSrcweir len++; 317cdf0e10cSrcweir } 318cdf0e10cSrcweir 319cdf0e10cSrcweir void 320cdf0e10cSrcweir Simstr::push_back(char c) 321cdf0e10cSrcweir { 322cdf0e10cSrcweir char * result = new char[len+2]; 323cdf0e10cSrcweir 324cdf0e10cSrcweir memcpy(result,sz,len); 325cdf0e10cSrcweir result[len] = c; 326cdf0e10cSrcweir result[len+1] = 0; 327cdf0e10cSrcweir 328cdf0e10cSrcweir delete [] sz; 329cdf0e10cSrcweir sz = result; 330cdf0e10cSrcweir len++; 331cdf0e10cSrcweir } 332cdf0e10cSrcweir 333cdf0e10cSrcweir void 334cdf0e10cSrcweir Simstr::insert(int pos, const Simstr & S) 335cdf0e10cSrcweir { 336cdf0e10cSrcweir if (pos < 0 || pos > len) 337cdf0e10cSrcweir return; 338cdf0e10cSrcweir 339cdf0e10cSrcweir char * result = new char[len+1+S.len]; 340cdf0e10cSrcweir 341cdf0e10cSrcweir memcpy(result,sz,pos); 342cdf0e10cSrcweir memcpy(result+pos,S.sz,S.len); 343cdf0e10cSrcweir memcpy(result+pos+S.len,sz+pos,len-pos+1); 344cdf0e10cSrcweir 345cdf0e10cSrcweir delete [] sz; 346cdf0e10cSrcweir sz = result; 347cdf0e10cSrcweir len += S.len; 348cdf0e10cSrcweir } 349cdf0e10cSrcweir 350cdf0e10cSrcweir void 351cdf0e10cSrcweir Simstr::push_front(const Simstr & S) 352cdf0e10cSrcweir { 353cdf0e10cSrcweir char * result = new char[len+1+S.len]; 354cdf0e10cSrcweir 355cdf0e10cSrcweir memcpy(result,S.sz,S.len); 356cdf0e10cSrcweir memcpy(result+S.len,sz,len+1); 357cdf0e10cSrcweir 358cdf0e10cSrcweir delete [] sz; 359cdf0e10cSrcweir sz = result; 360cdf0e10cSrcweir len += S.len; 361cdf0e10cSrcweir } 362cdf0e10cSrcweir 363cdf0e10cSrcweir void 364cdf0e10cSrcweir Simstr::push_back(const Simstr & S) 365cdf0e10cSrcweir { 366cdf0e10cSrcweir char * result = new char[len+1+S.len]; 367cdf0e10cSrcweir 368cdf0e10cSrcweir memcpy(result,sz,len); 369cdf0e10cSrcweir memcpy(result+len,S.sz,S.len+1); 370cdf0e10cSrcweir 371cdf0e10cSrcweir delete [] sz; 372cdf0e10cSrcweir sz = result; 373cdf0e10cSrcweir len += S.len; 374cdf0e10cSrcweir } 375cdf0e10cSrcweir 376cdf0e10cSrcweir 377cdf0e10cSrcweir // Remove 378cdf0e10cSrcweir 379cdf0e10cSrcweir void 380cdf0e10cSrcweir Simstr::remove(int pos, int anzahl) 381cdf0e10cSrcweir { 382cdf0e10cSrcweir if (pos >= len || pos < 0 || anzahl < 1) 383cdf0e10cSrcweir return; 384cdf0e10cSrcweir 385cdf0e10cSrcweir int anz = len - pos < anzahl ? len - pos : anzahl; 386cdf0e10cSrcweir 387cdf0e10cSrcweir char * result = new char[len-anz+1]; 388cdf0e10cSrcweir 389cdf0e10cSrcweir memcpy(result,sz,pos); 390cdf0e10cSrcweir memcpy(result+pos,sz+pos+anz,len-pos-anz+1); 391cdf0e10cSrcweir 392cdf0e10cSrcweir delete [] sz; 393cdf0e10cSrcweir sz = result; 394cdf0e10cSrcweir len -= anz; 395cdf0e10cSrcweir } 396cdf0e10cSrcweir 397cdf0e10cSrcweir void 398cdf0e10cSrcweir Simstr::remove_trailing_blanks() 399cdf0e10cSrcweir { 400cdf0e10cSrcweir int newlen = len-1; 401cdf0e10cSrcweir for ( ; newlen > 1 && sz[newlen] <= 32; --newlen ) {} 402cdf0e10cSrcweir 403cdf0e10cSrcweir if (newlen < len-1) 404cdf0e10cSrcweir remove ( newlen+1, len-newlen); 405cdf0e10cSrcweir } 406cdf0e10cSrcweir 407cdf0e10cSrcweir void 408cdf0e10cSrcweir Simstr::pop_front(int anzahl) 409cdf0e10cSrcweir { 410cdf0e10cSrcweir if (anzahl < 1) 411cdf0e10cSrcweir return; 412cdf0e10cSrcweir int anz = len < anzahl ? len : anzahl; 413cdf0e10cSrcweir 414cdf0e10cSrcweir char * result = new char[len-anz+1]; 415cdf0e10cSrcweir 416cdf0e10cSrcweir memcpy(result,sz+anz,len-anz+1); 417cdf0e10cSrcweir 418cdf0e10cSrcweir delete [] sz; 419cdf0e10cSrcweir sz = result; 420cdf0e10cSrcweir len -= anz; 421cdf0e10cSrcweir } 422cdf0e10cSrcweir 423cdf0e10cSrcweir void 424cdf0e10cSrcweir Simstr::pop_back(int anzahl) 425cdf0e10cSrcweir { 426cdf0e10cSrcweir if (anzahl < 1) 427cdf0e10cSrcweir return; 428cdf0e10cSrcweir 429cdf0e10cSrcweir int anz = len < anzahl ? len : anzahl; 430cdf0e10cSrcweir 431cdf0e10cSrcweir char * result = new char[len-anz+1]; 432cdf0e10cSrcweir 433cdf0e10cSrcweir memcpy(result,sz,len-anz); 434cdf0e10cSrcweir result[len-anz] = 0; 435cdf0e10cSrcweir 436cdf0e10cSrcweir delete [] sz; 437cdf0e10cSrcweir sz = result; 438cdf0e10cSrcweir len -= anz; 439cdf0e10cSrcweir } 440cdf0e10cSrcweir 441cdf0e10cSrcweir void 442cdf0e10cSrcweir Simstr::rem_back_from(int removeStartPos) 443cdf0e10cSrcweir { 444cdf0e10cSrcweir if (removeStartPos != NO_POS) 445cdf0e10cSrcweir pop_back(len-removeStartPos); 446cdf0e10cSrcweir } 447cdf0e10cSrcweir 448cdf0e10cSrcweir void 449cdf0e10cSrcweir Simstr::remove_all(char c) 450cdf0e10cSrcweir { 451cdf0e10cSrcweir if (!len) 452cdf0e10cSrcweir return; 453cdf0e10cSrcweir char * result = new char[len]; 454cdf0e10cSrcweir int i,j=0; 455cdf0e10cSrcweir for (i = 0; i < len; i++) 456cdf0e10cSrcweir if (sz[i] != c) 457cdf0e10cSrcweir result[j++] = sz[i]; 458cdf0e10cSrcweir 459cdf0e10cSrcweir delete [] sz; 460cdf0e10cSrcweir sz = new char[j+1]; 461cdf0e10cSrcweir memcpy(sz,result,j); 462cdf0e10cSrcweir sz[j] = 0; 463cdf0e10cSrcweir len = j; 464cdf0e10cSrcweir delete [] result; 465cdf0e10cSrcweir } 466cdf0e10cSrcweir 467cdf0e10cSrcweir void 468cdf0e10cSrcweir Simstr::remove_all(const Simstr & S) 469cdf0e10cSrcweir { 470cdf0e10cSrcweir int pos; 471cdf0e10cSrcweir while ( (pos=pos_first(S)) != NO_POS ) 472cdf0e10cSrcweir remove(pos,S.len); 473cdf0e10cSrcweir } 474cdf0e10cSrcweir 475cdf0e10cSrcweir void 476cdf0e10cSrcweir Simstr::strip(char c) 477cdf0e10cSrcweir { 478cdf0e10cSrcweir int start = 0; 479cdf0e10cSrcweir if (c == ' ') 480cdf0e10cSrcweir { // Sonderbehandlung: SPC entfernt auch TABs: 481cdf0e10cSrcweir while ( start < len 482cdf0e10cSrcweir ? sz[start] == ' ' 483cdf0e10cSrcweir || sz[start] == '\t' 484cdf0e10cSrcweir : false ) 485cdf0e10cSrcweir start++; 486cdf0e10cSrcweir } 487cdf0e10cSrcweir else 488cdf0e10cSrcweir { 489cdf0e10cSrcweir while (start < len && sz[start] == c) 490cdf0e10cSrcweir start++; 491cdf0e10cSrcweir } 492cdf0e10cSrcweir 493cdf0e10cSrcweir int ende = len-1; 494cdf0e10cSrcweir if (c == ' ') 495cdf0e10cSrcweir { // Sonderbehandlung: SPC entfernt auch TABs: 496cdf0e10cSrcweir while ( ende >= start 497cdf0e10cSrcweir ? sz[ende] == ' ' 498cdf0e10cSrcweir || sz[ende] == '\t' 499cdf0e10cSrcweir : false ) 500cdf0e10cSrcweir ende--; 501cdf0e10cSrcweir } 502cdf0e10cSrcweir else 503cdf0e10cSrcweir { 504cdf0e10cSrcweir while (ende >= start && sz[ende] == c) 505cdf0e10cSrcweir ende--; 506cdf0e10cSrcweir } 507cdf0e10cSrcweir *this = get(start,ende-start+1); 508cdf0e10cSrcweir } 509cdf0e10cSrcweir 510cdf0e10cSrcweir void 511cdf0e10cSrcweir Simstr::empty() 512cdf0e10cSrcweir { 513cdf0e10cSrcweir if (len > 0) 514cdf0e10cSrcweir { 515cdf0e10cSrcweir delete [] sz; 516cdf0e10cSrcweir sz = new char[1]; 517cdf0e10cSrcweir *sz = 0; 518cdf0e10cSrcweir len = 0; 519cdf0e10cSrcweir } 520cdf0e10cSrcweir } 521cdf0e10cSrcweir 522cdf0e10cSrcweir Simstr 523cdf0e10cSrcweir Simstr::take_first_token(char c) 524cdf0e10cSrcweir { 525cdf0e10cSrcweir Simstr ret; 526cdf0e10cSrcweir int pos = pos_first(c); 527cdf0e10cSrcweir if (pos != NO_POS) 528cdf0e10cSrcweir { 529cdf0e10cSrcweir ret = get_front(pos); 530cdf0e10cSrcweir pop_front(pos+1); 531cdf0e10cSrcweir } 532cdf0e10cSrcweir else 533cdf0e10cSrcweir { 534cdf0e10cSrcweir ret = sz; 535cdf0e10cSrcweir delete [] sz; 536cdf0e10cSrcweir sz = new char[1]; 537cdf0e10cSrcweir *sz = NULCH; 538cdf0e10cSrcweir len = 0; 539cdf0e10cSrcweir } 540cdf0e10cSrcweir 541cdf0e10cSrcweir return ret; 542cdf0e10cSrcweir } 543cdf0e10cSrcweir 544cdf0e10cSrcweir Simstr 545cdf0e10cSrcweir Simstr::take_last_token(char c) 546cdf0e10cSrcweir { 547cdf0e10cSrcweir Simstr ret; 548cdf0e10cSrcweir int pos = pos_last(c); 549cdf0e10cSrcweir if (pos != NO_POS) 550cdf0e10cSrcweir { 551cdf0e10cSrcweir ret = get_back(len-pos-1); 552cdf0e10cSrcweir pop_back(len-pos); 553cdf0e10cSrcweir } 554cdf0e10cSrcweir else 555cdf0e10cSrcweir { 556cdf0e10cSrcweir ret = sz; 557cdf0e10cSrcweir delete [] sz; 558cdf0e10cSrcweir sz = new char[1]; 559cdf0e10cSrcweir *sz = NULCH; 560cdf0e10cSrcweir len = 0; 561cdf0e10cSrcweir } 562cdf0e10cSrcweir 563cdf0e10cSrcweir return ret; 564cdf0e10cSrcweir } 565cdf0e10cSrcweir 566cdf0e10cSrcweir 567cdf0e10cSrcweir 568cdf0e10cSrcweir // Find 569cdf0e10cSrcweir 570cdf0e10cSrcweir int 571cdf0e10cSrcweir Simstr::pos_first(char c) const 572cdf0e10cSrcweir { 573cdf0e10cSrcweir int i = 0; 574cdf0e10cSrcweir for (i = 0; i < len ? sz[i] != c : false; i++) ; 575cdf0e10cSrcweir if (i >= len) 576cdf0e10cSrcweir return NO_POS; 577cdf0e10cSrcweir else 578cdf0e10cSrcweir return i; 579cdf0e10cSrcweir } 580cdf0e10cSrcweir 581cdf0e10cSrcweir int 582cdf0e10cSrcweir Simstr::pos_first_after( char c, 583cdf0e10cSrcweir int startSearchPos) const 584cdf0e10cSrcweir { 585cdf0e10cSrcweir int i = 0; 586cdf0e10cSrcweir if (startSearchPos >= i) 587cdf0e10cSrcweir i = startSearchPos+1; 588cdf0e10cSrcweir for (; i < len ? sz[i] != c : false; i++) ; 589cdf0e10cSrcweir if (i >= len) 590cdf0e10cSrcweir return NO_POS; 591cdf0e10cSrcweir else 592cdf0e10cSrcweir return i; 593cdf0e10cSrcweir } 594cdf0e10cSrcweir 595cdf0e10cSrcweir 596cdf0e10cSrcweir int 597cdf0e10cSrcweir Simstr::pos_last(char c) const 598cdf0e10cSrcweir { 599cdf0e10cSrcweir int i = 0; 600cdf0e10cSrcweir for (i = len-1; i >= 0 ? sz[i] != c : false; i--) ; 601cdf0e10cSrcweir if (i < 0) 602cdf0e10cSrcweir return NO_POS; 603cdf0e10cSrcweir else 604cdf0e10cSrcweir return i; 605cdf0e10cSrcweir } 606cdf0e10cSrcweir 607cdf0e10cSrcweir int 608cdf0e10cSrcweir Simstr::pos_first(const Simstr & S) const 609cdf0e10cSrcweir { 610cdf0e10cSrcweir char * ptr = strstr(sz,S.sz); 611cdf0e10cSrcweir if (ptr) 612cdf0e10cSrcweir return int(ptr-sz); 613cdf0e10cSrcweir else 614cdf0e10cSrcweir return NO_POS; 615cdf0e10cSrcweir } 616cdf0e10cSrcweir 617cdf0e10cSrcweir int 618cdf0e10cSrcweir Simstr::pos_last(const Simstr & S) const 619cdf0e10cSrcweir { 620cdf0e10cSrcweir Simstr vgl; 621cdf0e10cSrcweir int i; 622cdf0e10cSrcweir for (i = len-S.len; i >= 0 ; i--) 623cdf0e10cSrcweir { 624cdf0e10cSrcweir vgl = get(i,S.len); 625cdf0e10cSrcweir if (vgl == S) 626cdf0e10cSrcweir break; 627cdf0e10cSrcweir } 628cdf0e10cSrcweir if (i >= 0) 629cdf0e10cSrcweir return i; 630cdf0e10cSrcweir else 631cdf0e10cSrcweir return NO_POS; 632cdf0e10cSrcweir } 633cdf0e10cSrcweir 634cdf0e10cSrcweir int 635cdf0e10cSrcweir Simstr::count(char c) const 636cdf0e10cSrcweir { 637cdf0e10cSrcweir int ret = 0; 638cdf0e10cSrcweir for (int i =0; i < len; i++) 639cdf0e10cSrcweir if (sz[i] == c) 640cdf0e10cSrcweir ret++; 641cdf0e10cSrcweir return ret; 642cdf0e10cSrcweir } 643cdf0e10cSrcweir 644cdf0e10cSrcweir bool 645cdf0e10cSrcweir Simstr::is_no_text() const 646cdf0e10cSrcweir { 647cdf0e10cSrcweir if (!len) 648cdf0e10cSrcweir return true; 649cdf0e10cSrcweir 650cdf0e10cSrcweir int i; 651cdf0e10cSrcweir for (i = 0; sz[i] <= 32 && i < len; i++) ; 652cdf0e10cSrcweir if (i < len) 653cdf0e10cSrcweir return false; 654cdf0e10cSrcweir return true; 655cdf0e10cSrcweir } 656cdf0e10cSrcweir 657cdf0e10cSrcweir // Change 658cdf0e10cSrcweir 659cdf0e10cSrcweir void 660cdf0e10cSrcweir Simstr::replace(int pos, char c) 661cdf0e10cSrcweir { 662cdf0e10cSrcweir if (pos < 0 || pos >= len) 663cdf0e10cSrcweir return; 664cdf0e10cSrcweir else 665cdf0e10cSrcweir sz[unsigned(pos)] = c; 666cdf0e10cSrcweir } 667cdf0e10cSrcweir 668cdf0e10cSrcweir void 669cdf0e10cSrcweir Simstr::replace(int startPos, int anzahl, const Simstr & S) 670cdf0e10cSrcweir { 671cdf0e10cSrcweir if (startPos >= len || startPos < 0 || anzahl < 1) 672cdf0e10cSrcweir return; 673cdf0e10cSrcweir 674cdf0e10cSrcweir int anz = len - startPos < anzahl ? len - startPos : anzahl; 675cdf0e10cSrcweir 676cdf0e10cSrcweir char * result = new char[len-anz+S.len+1]; 677cdf0e10cSrcweir 678cdf0e10cSrcweir memcpy(result,sz,startPos); 679cdf0e10cSrcweir memcpy(result+startPos, S.sz, S.len); 680cdf0e10cSrcweir memcpy(result+startPos+S.len, sz+startPos+anz, len-startPos-anz+1); 681cdf0e10cSrcweir 682cdf0e10cSrcweir delete [] sz; 683cdf0e10cSrcweir sz = result; 684cdf0e10cSrcweir len = len-anz+S.len; 685cdf0e10cSrcweir } 686cdf0e10cSrcweir 687cdf0e10cSrcweir void 688cdf0e10cSrcweir Simstr::replace_all(char oldCh, char newCh) 689cdf0e10cSrcweir { 690cdf0e10cSrcweir for (int i=0; i < len; i++) 691cdf0e10cSrcweir if (sz[i] == oldCh) 692cdf0e10cSrcweir sz[i] = newCh; 693cdf0e10cSrcweir } 694cdf0e10cSrcweir 695cdf0e10cSrcweir void 696cdf0e10cSrcweir Simstr::replace_all(const Simstr & oldS, const Simstr & newS) 697cdf0e10cSrcweir { 698cdf0e10cSrcweir Simstr vgl; 699cdf0e10cSrcweir int i = 0; 700cdf0e10cSrcweir while (i <= len-oldS.len) 701cdf0e10cSrcweir { 702cdf0e10cSrcweir vgl = get(i,oldS.len); 703cdf0e10cSrcweir if (strcmp(vgl.sz,oldS.sz) == 0) 704cdf0e10cSrcweir { 705cdf0e10cSrcweir replace(i,oldS.len,newS); 706cdf0e10cSrcweir i += newS.len; 707cdf0e10cSrcweir } 708cdf0e10cSrcweir else 709cdf0e10cSrcweir i++; 710cdf0e10cSrcweir } 711cdf0e10cSrcweir } 712cdf0e10cSrcweir 713cdf0e10cSrcweir void 714cdf0e10cSrcweir Simstr::to_lower() 715cdf0e10cSrcweir { 716cdf0e10cSrcweir for (int i = 0; i < len; i++) 717cdf0e10cSrcweir sz[i] = (char) tolower(sz[i]); 718cdf0e10cSrcweir } 719cdf0e10cSrcweir 720cdf0e10cSrcweir 721cdf0e10cSrcweir 722cdf0e10cSrcweir // Simstr addition 723cdf0e10cSrcweir Simstr 724cdf0e10cSrcweir operator+(const char * str, const Simstr & S) 725cdf0e10cSrcweir { 726cdf0e10cSrcweir Simstr ret = S; 727cdf0e10cSrcweir ret.push_front(str); 728cdf0e10cSrcweir return ret; 729cdf0e10cSrcweir } 730cdf0e10cSrcweir 731cdf0e10cSrcweir Simstr 732cdf0e10cSrcweir operator+(const Simstr & S, const char * str) 733cdf0e10cSrcweir { 734cdf0e10cSrcweir Simstr ret = S; 735cdf0e10cSrcweir ret.push_back(str); 736cdf0e10cSrcweir return ret; 737cdf0e10cSrcweir } 738cdf0e10cSrcweir 739cdf0e10cSrcweir Simstr 740cdf0e10cSrcweir operator+(char c, const Simstr & S) 741cdf0e10cSrcweir { 742cdf0e10cSrcweir Simstr ret = S; 743cdf0e10cSrcweir ret.push_front(c); 744cdf0e10cSrcweir return ret; 745cdf0e10cSrcweir } 746cdf0e10cSrcweir 747cdf0e10cSrcweir Simstr 748cdf0e10cSrcweir operator+(const Simstr & S, char c) 749cdf0e10cSrcweir { 750cdf0e10cSrcweir Simstr ret = S; 751cdf0e10cSrcweir ret.push_back(c); 752cdf0e10cSrcweir return ret; 753cdf0e10cSrcweir } 754cdf0e10cSrcweir 755cdf0e10cSrcweir 756cdf0e10cSrcweir // Simstr-Vergleiche mit char * 757cdf0e10cSrcweir bool 758cdf0e10cSrcweir operator==(const Simstr & S, const char * str) 759cdf0e10cSrcweir { 760cdf0e10cSrcweir return strcmp(S,str) == 0; 761cdf0e10cSrcweir } 762cdf0e10cSrcweir 763cdf0e10cSrcweir bool 764cdf0e10cSrcweir operator!=(const Simstr & S, const char * str) 765cdf0e10cSrcweir { 766cdf0e10cSrcweir return strcmp(S,str) != 0; 767cdf0e10cSrcweir } 768cdf0e10cSrcweir 769cdf0e10cSrcweir bool 770cdf0e10cSrcweir operator<(const Simstr & S, const char * str) 771cdf0e10cSrcweir { 772cdf0e10cSrcweir return strcmp(S,str) < 0; 773cdf0e10cSrcweir } 774cdf0e10cSrcweir 775cdf0e10cSrcweir bool 776cdf0e10cSrcweir operator>(const Simstr & S, const char * str) 777cdf0e10cSrcweir { 778cdf0e10cSrcweir return strcmp(S,str) > 0; 779cdf0e10cSrcweir } 780cdf0e10cSrcweir 781cdf0e10cSrcweir bool 782cdf0e10cSrcweir operator<=(const Simstr & S, const char * str) 783cdf0e10cSrcweir { 784cdf0e10cSrcweir return strcmp(S,str) <= 0; 785cdf0e10cSrcweir } 786cdf0e10cSrcweir 787cdf0e10cSrcweir bool 788cdf0e10cSrcweir operator>=(const Simstr & S, const char * str) 789cdf0e10cSrcweir { 790cdf0e10cSrcweir return strcmp(S,str) >= 0; 791cdf0e10cSrcweir } 792cdf0e10cSrcweir 793cdf0e10cSrcweir bool 794cdf0e10cSrcweir operator==(const char * str, const Simstr & S) 795cdf0e10cSrcweir { 796cdf0e10cSrcweir return strcmp(str,S) == 0; 797cdf0e10cSrcweir } 798cdf0e10cSrcweir 799cdf0e10cSrcweir bool 800cdf0e10cSrcweir operator!=(const char * str, const Simstr & S) 801cdf0e10cSrcweir { 802cdf0e10cSrcweir return strcmp(str,S) != 0; 803cdf0e10cSrcweir } 804cdf0e10cSrcweir 805cdf0e10cSrcweir bool 806cdf0e10cSrcweir operator<(const char * str, const Simstr & S) 807cdf0e10cSrcweir { 808cdf0e10cSrcweir return strcmp(str,S) < 0; 809cdf0e10cSrcweir } 810cdf0e10cSrcweir 811cdf0e10cSrcweir bool 812cdf0e10cSrcweir operator>(const char * str, const Simstr & S) 813cdf0e10cSrcweir { 814cdf0e10cSrcweir return strcmp(str,S) > 0; 815cdf0e10cSrcweir } 816cdf0e10cSrcweir 817cdf0e10cSrcweir bool 818cdf0e10cSrcweir operator<=(const char * str, const Simstr & S) 819cdf0e10cSrcweir { 820cdf0e10cSrcweir return strcmp(str,S) <= 0; 821cdf0e10cSrcweir } 822cdf0e10cSrcweir 823cdf0e10cSrcweir bool 824cdf0e10cSrcweir operator>=(const char * str, const Simstr & S) 825cdf0e10cSrcweir { 826cdf0e10cSrcweir return strcmp(str,S) >= 0; 827cdf0e10cSrcweir } 828cdf0e10cSrcweir 829cdf0e10cSrcweir 830