Subversion Repositories spk

Rev

Rev 85 | Rev 94 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 85 Rev 86
Line 237... Line 237...
237
{
237
{
238
	if ( this->empty() ) return "";
238
	if ( this->empty() ) return "";
239

239

240
	int max = this->countToken(token);
240
	int max = this->countToken(token);
241

241

242
	std::string newstr;
242
	Utils::String newstr;
243

243

244
	int whichtok = 1;
244
	int whichtok = 1;
245
	std::string::size_type lastPos = 0;
245
	Utils::String::size_type lastPos = 0;
246
	std::string::size_type pos     = _token_nextPos(token, 0);
246
	Utils::String::size_type pos     = _token_nextPos(token, 0);
247

247

248
	if ( from < 0 )
248
	if ( from < 0 )
249
		from = max + (from + 1);
249
		from = max + (from + 1);
250
	if ( to < 0 )
250
	if ( to < 0 )
251
		to = max + (to + 1);
251
		to = max + (to + 1);
252

252

253
	while (std::string::npos != pos || std::string::npos != lastPos)
253
	while (Utils::String::npos != pos || Utils::String::npos != lastPos)
254
	{
254
	{
255
		if ( to == 0 )
255
		if ( to == 0 )
256
		{
256
		{
257
			if ( whichtok >= from )
257
			if ( whichtok >= from )
258
			{
258
			{
Line 292... Line 292...
292
	return newstr;
292
	return newstr;
293
}
293
}
294

294

295
String String::remToken(const char *token, int from) const
295
String String::remToken(const char *token, int from) const
296
{
296
{
297
	std::string newstr;
297
	Utils::String newstr;
298

298

299
	int whichtok = 1;
299
	int whichtok = 1;
300
	std::string::size_type lastPos = 0;
300
	Utils::String::size_type lastPos = 0;
301
	std::string::size_type pos     = _token_nextPos(token, 0);
301
	Utils::String::size_type pos     = _token_nextPos(token, 0);
302

302

303
	while (std::string::npos != pos || std::string::npos != lastPos)
303
	while (Utils::String::npos != pos || Utils::String::npos != lastPos)
304
	{
304
	{
305
		if ( whichtok != from )
305
		if ( whichtok != from )
306
		{
306
		{
307
			if ( newstr != "" ) newstr = newstr + token;
307
			if ( newstr != "" ) newstr = newstr + token;
308
			newstr = newstr + this->substr(lastPos, pos - lastPos);
308
			newstr = newstr + this->substr(lastPos, pos - lastPos);
Line 344... Line 344...
344
		return 0;
344
		return 0;
345

345

346
	// finds the number of tokens in a string
346
	// finds the number of tokens in a string
347
	int found = 1;
347
	int found = 1;
348

348

349
	std::string tmpstr = *this;
349
	Utils::String tmpstr = *this;
350

350

351
	// ignore the initial spaces
351
	// ignore the initial spaces
352
	std::string::size_type pos = this->find_first_not_of(token, 0);
352
	Utils::String::size_type pos = this->find_first_not_of(token, 0);
353

353

354
	// remove end spaces
354
	// remove end spaces
355
	size_t i = tmpstr.size() - 1;
355
	size_t i = tmpstr.size() - 1;
356
	while ( tmpstr[i] == ' ' && i > 0 )
356
	while ( tmpstr[i] == ' ' && i > 0 )
357
		i--;
357
		i--;
358
	if ( i <= 0 ) return 0;
358
	if ( i <= 0 ) return 0;
359
	tmpstr.erase ( i + 1, tmpstr.size() - i );
359
	tmpstr.erase ( i + 1, tmpstr.size() - i );
360

360

361
	// count tokens
361
	// count tokens
362
	pos = tmpstr.find (token,pos + 1);
362
	pos = tmpstr.find (token,pos + 1);
363
	while ( pos < std::string::npos )
363
	while ( pos < Utils::String::npos )
364
	{
364
	{
365
		while (tmpstr[pos] == ' ')
365
		while (tmpstr[pos] == ' ')
366
			pos++;
366
			pos++;
367
		found++;
367
		found++;
368
		pos = tmpstr.find (token,pos + 1);
368
		pos = tmpstr.find (token,pos + 1);
Line 370... Line 370...
370

370

371
	// return number of tokens
371
	// return number of tokens
372
	return found;
372
	return found;
373
}
373
}
374

374

375
std::string::size_type String::_token_nextPos(const char *token, std::string::size_type curPos) const
375
Utils::String::size_type String::_token_nextPos(const char *token, Utils::String::size_type curPos) const
376
{
376
{
377
	bool found = false;
377
	bool found = false;
378
	std::string::size_type startPos = 0;
378
	Utils::String::size_type startPos = 0;
379
	size_t max = strlen(token);
379
	size_t max = strlen(token);
380
	while ( !found ) {
380
	while ( !found ) {
381
		found = true;
381
		found = true;
382
		startPos = this->find_first_of(token[0], curPos);
382
		startPos = this->find_first_of(token[0], curPos);
383
		if ( startPos == std::string::npos ) {
383
		if ( startPos == Utils::String::npos ) {
384
			startPos = this->length();
384
			startPos = this->length();
385
			break;
385
			break;
386
		}
386
		}
387

387

388
		std::string::size_type pos = startPos;
388
		Utils::String::size_type pos = startPos;
389
		size_t i = 1;
389
		size_t i = 1;
390
		while ( i < max )
390
		while ( i < max )
391
		{
391
		{
392
			++pos;
392
			++pos;
393
			if ( this->at(pos) != token[i] )
393
			if ( this->at(pos) != token[i] )
Line 402... Line 402...
402
	return startPos;
402
	return startPos;
403
}
403
}
404

404

405
String String::findReplace(const String &find, const String &replace ) const
405
String String::findReplace(const String &find, const String &replace ) const
406
{
406
{
407
	std::string newstr = *this;
407
	Utils::String newstr = *this;
408
	std::string::size_type pos = newstr.find(find, 0);
408
	Utils::String::size_type pos = newstr.find(find, 0);
409
	while ( pos < std::string::npos ) {
409
	while ( pos < Utils::String::npos ) {
410
		newstr.replace(pos, find.length(), replace);
410
		newstr.replace(pos, find.length(), replace);
411
		pos = newstr.find(find, pos + replace.length());
411
		pos = newstr.find(find, pos + replace.length());
412
	}
412
	}
413

413

414
	return newstr;
414
	return newstr;
Line 416... Line 416...
416

416

417
String String::findRemove(const String &find) const
417
String String::findRemove(const String &find) const
418
{
418
{
419
	Utils::String replace = *this;
419
	Utils::String replace = *this;
420

420

421
	std::string::size_type pos = replace.find(find, 0 );
421
	Utils::String::size_type pos = replace.find(find, 0 );
422
	while ( pos != std::string::npos ) {
422
	while ( pos != Utils::String::npos ) {
423
		replace.erase ( pos, find.length() );
423
		replace.erase ( pos, find.length() );
424
		pos = replace.find(find, pos);
424
		pos = replace.find(find, pos);
425
	}
425
	}
426

426

427
	return replace;
427
	return replace;
Line 446... Line 446...
446
	newStr = newStr.findReplace("<br />", "\n");
446
	newStr = newStr.findReplace("<br />", "\n");
447
	newStr = newStr.findReplace("<p>", "\n\t");
447
	newStr = newStr.findReplace("<p>", "\n\t");
448
	newStr = newStr.findReplace("</p>", "\n");
448
	newStr = newStr.findReplace("</p>", "\n");
449

449

450
	// find any remaining tags and remove them
450
	// find any remaining tags and remove them
451
	std::string::size_type pos     = newStr.find_first_of('<', 0);
451
	Utils::String::size_type pos     = newStr.find_first_of('<', 0);
452
	std::string::size_type lastPos = newStr.find_first_of('>', pos);
452
	Utils::String::size_type lastPos = newStr.find_first_of('>', pos);
453

453

454
	while (std::string::npos != pos || std::string::npos != lastPos)
454
	while (Utils::String::npos != pos || Utils::String::npos != lastPos)
455
	{
455
	{
456
		if ( lastPos > newStr.length() ) break;
456
		if ( lastPos > newStr.length() ) break;
457
		// remove the tag
457
		// remove the tag
458
		newStr.erase(pos, lastPos + 1);
458
		newStr.erase(pos, lastPos + 1);
459

459

Line 467... Line 467...
467
	return newStr;
467
	return newStr;
468
}
468
}
469

469

470
bool String::isin(const String &str, bool bCaseSensative) const
470
bool String::isin(const String &str, bool bCaseSensative) const
471
{
471
{
472
	if ( bCaseSensative ) return (this->find(str, 0) == std::string::npos) ? false : true;
472
	if ( bCaseSensative ) return (this->find(str, 0) == Utils::String::npos) ? false : true;
473
	
473
	
474
	String thisLower = this->lower();
474
	String thisLower = this->lower();
475
	String strLower = str.lower();
475
	String strLower = str.lower();
476
	return (thisLower.find(strLower, 0) == std::string::npos) ? false : true;
476
	return (thisLower.find(strLower, 0) == Utils::String::npos) ? false : true;
477
}
477
}
478

478

479
int String::findPos(const String &find, int iStartPos) const
479
int String::findPos(const String &find, int iStartPos) const
480
{
480
{
481
	std::string::size_type pos = this->find(find, iStartPos);
481
	Utils::String::size_type pos = this->find(find, iStartPos);
482
	if ( pos == std::string::npos ) return -1;
482
	if ( pos == Utils::String::npos ) return -1;
483
	return pos;
483
	return pos;
484
}
484
}
485

485

486
bool String::isin(char c, bool bCaseSensative) const
486
bool String::isin(char c, bool bCaseSensative) const
487
{
487
{
Line 589... Line 589...
589
	return data + (pos + 1);
589
	return data + (pos + 1);
590
}
590
}
591

591

592
String String::replaceToken(const char *token, int from, const String &replace) const
592
String String::replaceToken(const char *token, int from, const String &replace) const
593
{
593
{
594
	std::string newstr;
594
	Utils::String newstr;
595

595

596
	int whichtok = 1;
596
	int whichtok = 1;
597
	std::string::size_type lastPos = 0;
597
	Utils::String::size_type lastPos = 0;
598
	std::string::size_type pos     = _token_nextPos(token, 0);
598
	Utils::String::size_type pos     = _token_nextPos(token, 0);
599

599

600
	while (std::string::npos != pos || std::string::npos != lastPos)
600
	while (Utils::String::npos != pos || Utils::String::npos != lastPos)
601
	{
601
	{
602
		if ( whichtok == from )
602
		if ( whichtok == from )
603
		{
603
		{
604
			if ( newstr != "" ) newstr = newstr + token;
604
			if ( newstr != "" ) newstr = newstr + token;
605
			newstr = newstr + replace;
605
			newstr = newstr + replace;
Line 649... Line 649...
649

649

650
	String *newstr = (*max == 1) ? new String : new String[*max];
650
	String *newstr = (*max == 1) ? new String : new String[*max];
651

651

652
	int whichtok = 0;
652
	int whichtok = 0;
653
	// get first token
653
	// get first token
654
	std::string::size_type lastPos = 0;
654
	Utils::String::size_type lastPos = 0;
655
	std::string::size_type pos     = this->_token_nextPos(token, 0);
655
	Utils::String::size_type pos     = this->_token_nextPos(token, 0);
656

656

657
	while (std::string::npos != pos || std::string::npos != lastPos)
657
	while (Utils::String::npos != pos || Utils::String::npos != lastPos)
658
	{
658
	{
659
		// set token
659
		// set token
660
		if ( *max == 1 ) *newstr = this->substr(lastPos, pos - lastPos);
660
		if ( *max == 1 ) *newstr = this->substr(lastPos, pos - lastPos);
661
		else			  newstr[whichtok] = this->substr(lastPos, pos - lastPos);
661
		else			  newstr[whichtok] = this->substr(lastPos, pos - lastPos);
662

662

Line 695... Line 695...
695
	return (*this);
695
	return (*this);
696
}
696
}
697

697

698
const String &String::removeChar(char c)
698
const String &String::removeChar(char c)
699
{
699
{
700
	std::string::size_type pos = this->find_first_of(c, 0);
700
	Utils::String::size_type pos = this->find_first_of(c, 0);
701
	while(pos != std::string::npos) {
701
	while(pos != Utils::String::npos) {
702
		this->erase(pos, 1);
702
		this->erase(pos, 1);
703
		pos = this->find_first_of(c, 0);
703
		pos = this->find_first_of(c, 0);
704
	}
704
	}
705

705

706
	return (*this);
706
	return (*this);
Line 708... Line 708...
708

708

709
String String::remove(char c) const
709
String String::remove(char c) const
710
{
710
{
711
	String newStr = *this;
711
	String newStr = *this;
712

712

713
	std::string::size_type pos = newStr.find_first_of(c, 0);
713
	Utils::String::size_type pos = newStr.find_first_of(c, 0);
714
	while(pos != std::string::npos) {
714
	while(pos != Utils::String::npos) {
715
		newStr.erase(pos, 1);
715
		newStr.erase(pos, 1);
716
		pos = newStr.find_first_of(c, 0);
716
		pos = newStr.find_first_of(c, 0);
717
	}
717
	}
718

718

719
	return newStr;
719
	return newStr;
Line 743... Line 743...
743
	return true;
743
	return true;
744
}
744
}
745

745

746
const String &String::removeFirstSpace()
746
const String &String::removeFirstSpace()
747
{
747
{
748
	std::string::size_type pos = this->find_first_not_of(" \t\f\v\n\r", 0);
748
	Utils::String::size_type pos = this->find_first_not_of(" \t\f\v\n\r", 0);
749
	if ( pos != std::string::npos ) {
749
	if ( pos != Utils::String::npos ) {
750
		this->erase(0, pos);
750
		this->erase(0, pos);
751
	}
751
	}
752
	return (*this);
752
	return (*this);
753
}
753
}
754
const String &String::removeEndSpace()
754
const String &String::removeEndSpace()
755
{
755
{
756
	std::string::size_type pos = this->find_last_not_of(" \t\f\v\n\r");
756
	Utils::String::size_type pos = this->find_last_not_of(" \t\f\v\n\r");
757
	if ( pos != std::string::npos ) {
757
	if ( pos != Utils::String::npos ) {
758
		this->erase(pos + 1);
758
		this->erase(pos + 1);
759
	}
759
	}
760
	return (*this);
760
	return (*this);
761
}
761
}
762

762