1 /* $XConsortium: cstring.C /main/2 1995/07/17 14:10:22 drk $ */
2 /*******************************************************************
3 ** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
4 ** All rights are reserved. Copying or other reproduction of this
5 ** program except for archival purposes is prohibited without prior
6 ** written consent of Hewlett-Packard Company.
7 ********************************************************************
8 ****************************<+>*************************************/
23 ) : skipWhiteSpace(ws)
26 contents = new char [strlen(s) + 1];
30 contents = new char [1];
40 ) : skipWhiteSpace(ws)
42 contents = new char [2];
57 contents = new char [s.length() + 1];
58 strcpy(contents,s.data());
59 skipWhiteSpace = s.skipWhiteSpace;
62 char & CString::operator[]
70 if (index > strlen(contents))
71 return contents[strlen(contents)-1];
73 return contents[index];
76 int CString::operator!=
84 int CString::operator==
89 if (isNull() && s.isNull())
92 if (isNull() || s.isNull())
95 if (strcmp(contents,s.data()) == 0)
101 CString & CString::operator=
108 contents = new char [s.length() + 1];
109 strcpy(contents,s.data());
114 CString & CString::operator=
120 contents = new char [strlen(s) + 1];
125 CString & CString::operator+=
134 temp = new char [strlen(contents) + strlen(cs) + 1];
140 temp = new char [strlen(cs) + 1];
147 strcat(temp,contents);
158 CString & CString::operator+=
167 temp = new char [strlen(contents) + s.length() + 1];
173 temp = new char [s.length() + 1];
180 strcat(temp,contents);
182 strcat(temp,s.data());
230 CString CString::copy
239 for (int i = 0; i < strlen(delim); i++) {
241 if (q = strchr(contents,delim[i])) {
244 CString result(&contents[start]);
252 CString CString::copy
261 for (int i = 0; i < strlen(delim1); i++) {
263 if (q = strchr(contents,delim1[i])) {
264 for (int j = 0; j < strlen(delim2); j++) {
266 if (p = strchr(q+1,delim2[j])) {
281 CString CString::find
291 if (q = strstr(contents,cs))
297 int CString::contains
304 return contains(s.data(), leader, trailer);
307 int CString::contains
314 // first see if there is anything in contents
319 // or if the strings are exactly the same
321 if (strcmp(cs,data()) == 0)
324 // now check to see if it is embedded in the string
326 CString search(leader);
330 if (strstr (contents, search.data()))
333 // if skipWhiteSpace is on then check for spaces on one or
334 // both sides of the target in the source.
336 if (skipWhiteSpace) {
338 // first, a space in front, delimiter on the end
343 if (strstr (contents, search.data()))
346 // second, a delimiter on the front, space on the end
351 if (strstr (contents, search.data()))
354 // third, spaces before and after
359 if (strstr (contents, search.data()))
363 // now check to see if it is the start of the string
367 if (strncmp (contents, search.data(), search.length()) == 0)
370 // now check to see if it is the start of the string followed
375 if (strncmp (contents, search.data(), search.length()) == 0)
378 // last, check to see if it is on the end of the string using
379 // the delimiter and a space
386 while (q = strstr(p, search.data())) {
387 r = q + strlen(leader);
390 if (r && strcmp(r,cs) == 0)
393 if (search[0] == ' ')
402 void CString::replace
404 const CString & to_be_replaced,
405 const CString & replacee
413 if (q = strstr(contents,to_be_replaced.data())) {
415 char * prefix = new char [strlen(contents) + 1];
416 strcpy(prefix,contents);
417 q += to_be_replaced.length();
418 char * suffix = new char [strlen(q) + 1];
421 contents = new char [strlen(prefix) +
424 strcpy(contents,prefix);
425 strcat(contents,replacee.data());
426 strcat(contents,suffix);
432 int CString::isNull() const
437 if (strlen(contents) == 0)
444 CTokenizedString::CTokenizedString()
445 : CString(""), finished(0), cursor(0), delimiter(0)
449 CTokenizedString::CTokenizedString
451 const CTokenizedString & s
455 cursor = new char [strlen(s.cursor) + 1];
456 strcpy(cursor,s.cursor);
457 delimiter = new char [strlen(s.delimiter) + 1];
458 strcpy(delimiter,s.delimiter);
459 finished = s.finished;
460 skipWhiteSpace = s.skipWhiteSpace;
461 contents = new char [strlen(s.contents) + 1];
462 strcpy(contents,s.contents);
466 CTokenizedString::CTokenizedString
471 ) : CString(s), finished(0)
473 cursor = new char [s.length() + 1];
474 strcpy(cursor,s.data());
475 delimiter = new char [strlen(d) + 1];
481 CTokenizedString::~CTokenizedString()
488 CString CTokenizedString::next()
493 if (strlen(delimiter) == 1)
494 q = strchr(cursor,delimiter[0]);
496 for (int i = 0; i < strlen(cursor); i++)
497 if (strchr(delimiter,cursor[i])) {
506 // a delimiter has been found. Create the return token,
511 // eliminate trailing white space
513 if (skipWhiteSpace) {
514 for (char *p = q; isspace(*(p-1)); p--);
517 CString result(cursor);
519 // eliminate leading white space
522 for (; isspace(*(q+1)); q++);
524 char * temp = new char [strlen(q+1) + 1];
532 // no delimiter found. Return what is left of the string
534 CString result(cursor);
552 return os << "(null)";
554 return os << s.data();