Browse Source

cmText.h/c : Added cmTextVAppendSS(), cmTextLength(), cmTextCmpN(), cmTextToLower(), cmTextToUpper().

master
kevin 10 years ago
parent
commit
bc7b5a4c09
2 changed files with 91 additions and 2 deletions
  1. 76
    2
      cmText.c
  2. 15
    0
      cmText.h

+ 76
- 2
cmText.c View File

535
 cmChar_t* _cmTextReplace( cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned n )
535
 cmChar_t* _cmTextReplace( cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned n )
536
 {
536
 {
537
   // we will go into an endless loop if 't' is contained in 'u' and n > 1.
537
   // we will go into an endless loop if 't' is contained in 'u' and n > 1.
538
-  assert( s!= NULL && t!=NULL && u!=NULL && (n==1 || strstr(u,t) == NULL) );
538
+  //assert( s!= NULL && t!=NULL && u!=NULL && (n==1 || strstr(u,t) == NULL) );
539
+
540
+  assert( s!= NULL && t!=NULL && u!=NULL );
539
 
541
 
540
   int       tn = strlen(t);
542
   int       tn = strlen(t);
541
   cmChar_t* c  = NULL;
543
   cmChar_t* c  = NULL;
542
   unsigned  i  = 0;
544
   unsigned  i  = 0;
545
+  cmChar_t* s0 = s;
543
 
546
 
544
-  while( (c = strstr(s,t)) != NULL ) 
547
+  while( (c = strstr(s0,t)) != NULL ) 
545
   {
548
   {
549
+    int offs = c - s;
546
     s = cmTextReplaceS(s,c,tn,u);
550
     s = cmTextReplaceS(s,c,tn,u);
547
 
551
 
548
     assert(s!=NULL);
552
     assert(s!=NULL);
549
 
553
 
554
+    s0 = s + offs + tn;
555
+
550
     ++i;
556
     ++i;
551
     if( n!=cmInvalidCnt && i>=n)
557
     if( n!=cmInvalidCnt && i>=n)
552
       break;
558
       break;
599
 cmChar_t* cmTextAppendSS( cmChar_t* s, const cmChar_t* u )
605
 cmChar_t* cmTextAppendSS( cmChar_t* s, const cmChar_t* u )
600
 { return cmTextAppendSN(s,u,strlen(u)); }
606
 { return cmTextAppendSN(s,u,strlen(u)); }
601
 
607
 
608
+cmChar_t* cmTextVAppendSS( cmChar_t* s, ... )
609
+{
610
+  va_list vl;
611
+  va_start(vl,s);
612
+  do
613
+  {
614
+    cmChar_t* s0 = va_arg(vl,cmChar_t*);
615
+    if( s0 == NULL )
616
+      break;
617
+
618
+    s = cmTextAppendSS(s,s0);
619
+
620
+  }while(1);
621
+
622
+  va_end(vl);
623
+
624
+  return s;
625
+}
626
+
627
+
602
 
628
 
603
 cmChar_t* cmTextAppendChar( cmChar_t* s, cmChar_t c, unsigned n )
629
 cmChar_t* cmTextAppendChar( cmChar_t* s, cmChar_t c, unsigned n )
604
 {
630
 {
623
 bool cmTextIsNotEmpty( const cmChar_t* s )
649
 bool cmTextIsNotEmpty( const cmChar_t* s )
624
 { return !cmTextIsEmpty(s); }
650
 { return !cmTextIsEmpty(s); }
625
 
651
 
652
+
653
+unsigned cmTextLength( const cmChar_t* s0 )
654
+{
655
+  if( s0 == NULL )
656
+    return 0;
657
+  return strlen(s0);
658
+}
659
+
626
 int cmTextCmp( const cmChar_t* s0, const cmChar_t* s1 )
660
 int cmTextCmp( const cmChar_t* s0, const cmChar_t* s1 )
627
 {
661
 {
628
   if( s0 == NULL && s1 == NULL )
662
   if( s0 == NULL && s1 == NULL )
638
   return strcmp(s0,s1);
672
   return strcmp(s0,s1);
639
 }
673
 }
640
 
674
 
675
+int cmTextCmpN( const cmChar_t* s0, const cmChar_t* s1, unsigned n )
676
+{
677
+  if( s0 == NULL && s1 == NULL )
678
+    return 0;
679
+
680
+  if( s0 == NULL || s1 == NULL )
681
+  {
682
+    if( s0 == NULL )
683
+      return -1;
684
+    return 1;
685
+  }
686
+
687
+  return strncmp(s0,s1,n);
688
+}
689
+
690
+
691
+void cmTextToLower( const cmChar_t* s0, cmChar_t* s1 )
692
+{
693
+  if( s0 == NULL || s1==NULL )
694
+    return;
695
+
696
+  for(; *s0; ++s0,++s1)
697
+    *s1 = tolower(*s0);
698
+
699
+  *s1 = 0;
700
+  return;
701
+}
702
+
703
+void cmTextToUpper( const cmChar_t* s0, cmChar_t* s1 )
704
+{
705
+  if( s0 == NULL || s1==NULL )
706
+    return;
707
+
708
+  for(; *s0; ++s0,++s1)
709
+    *s1 = toupper(*s0);
710
+
711
+  *s1 = 0;
712
+  return;
713
+}
714
+
641
 cmChar_t* cmTextLine( cmChar_t* s, unsigned line )
715
 cmChar_t* cmTextLine( cmChar_t* s, unsigned line )
642
 {
716
 {
643
   assert( line>0);
717
   assert( line>0);

+ 15
- 0
cmText.h View File

192
   // both s[] and u[] are strz's
192
   // both s[] and u[] are strz's
193
   cmChar_t* cmTextAppendSS( cmChar_t* s, const cmChar_t* u );
193
   cmChar_t* cmTextAppendSS( cmChar_t* s, const cmChar_t* u );
194
 
194
 
195
+  // Same as multiple calls to cmTextAppendSS(). 
196
+  // Terminate the var-args list with NULL.
197
+  cmChar_t* cmTextVAppendSS( cmChar_t* s, ... );
198
+
195
   // Append 'n' copies of 'c' to the end of s[].
199
   // Append 'n' copies of 'c' to the end of s[].
196
   cmChar_t* cmTextAppendChar( cmChar_t* s, cmChar_t c, unsigned n );
200
   cmChar_t* cmTextAppendChar( cmChar_t* s, cmChar_t c, unsigned n );
197
 
201
 
199
   bool cmTextIsEmpty( const cmChar_t* s );
203
   bool cmTextIsEmpty( const cmChar_t* s );
200
   bool cmTextIsNotEmpty( const cmChar_t* s );
204
   bool cmTextIsNotEmpty( const cmChar_t* s );
201
 
205
 
206
+  // Same as strlen() but handles case where s0 == NULL as length==0.
207
+  unsigned cmTextLength( const cmChar_t* s0 );
208
+
202
   // Same as strcmp() but handles NULL.  Note that if both s0 and s1 are NULL
209
   // Same as strcmp() but handles NULL.  Note that if both s0 and s1 are NULL
203
   // then return is 0.
210
   // then return is 0.
204
   int cmTextCmp( const cmChar_t* s0, const cmChar_t* s1 );
211
   int cmTextCmp( const cmChar_t* s0, const cmChar_t* s1 );
205
 
212
 
213
+  // Same as cmTextCmp() but only compare the first 'n' characters.
214
+  int cmTextCmpN( const cmChar_t* s0, const cmChar_t* s1, unsigned n );
215
+
216
+  // Convert text in s0[] to upper/lower case in s1[].
217
+  // Note that s0[] and s1[] may point to the same string
218
+  void cmTextToLower( const cmChar_t* s0, cmChar_t* s1 );
219
+  void cmTextToUpper( const cmChar_t* s0, cmChar_t* s1 );
220
+
206
   // Returns NULL if string contains fewer than lineIdx lines.
221
   // Returns NULL if string contains fewer than lineIdx lines.
207
   // Note: first line == 1.
222
   // Note: first line == 1.
208
   cmChar_t* cmTextLine( cmChar_t* s, unsigned line );
223
   cmChar_t* cmTextLine( cmChar_t* s, unsigned line );

Loading…
Cancel
Save