浏览代码

cmText.h/c : Added cmTextTrimBegin(),cmTextTrimEnd() and cmTextTime().

master
kevin 11 年前
父节点
当前提交
84782a1fa8
共有 2 个文件被更改,包括 48 次插入0 次删除
  1. 43
    0
      cmText.c
  2. 5
    0
      cmText.h

+ 43
- 0
cmText.c 查看文件

@@ -519,6 +519,49 @@ void cmTextClip(    cmChar_t* s, unsigned n )
519 519
     
520 520
 }
521 521
 
522
+cmChar_t* cmTextTrimBegin( cmChar_t* s )
523
+{
524
+  if( s==NULL || strlen(s) == 0 )
525
+    return s;
526
+
527
+  cmChar_t* s0 = cmTextNextNonWhite(s);
528
+
529
+  // no non-white char's exist
530
+  if( s0 == NULL )
531
+  {
532
+    s[0] = 0;
533
+    return s;
534
+  }
535
+
536
+  if( s0 != s )
537
+    cmTextShrinkS(s,s,s0-s);
538
+
539
+  return s;
540
+}
541
+
542
+cmChar_t* cmTextTrimEnd( cmChar_t* s )
543
+{
544
+  unsigned sn;
545
+
546
+  if( s==NULL || (sn = strlen(s))==0)
547
+    return s;
548
+
549
+  cmChar_t* s0 = cmTextLastNonWhiteChar(s);
550
+
551
+  if(s0-s+1 < sn )
552
+    s[s0-s+1] = 0;
553
+
554
+
555
+  return s;
556
+}
557
+
558
+cmChar_t* cmTextTrim( cmChar_t* s)
559
+{
560
+  cmTextTrimBegin(s);
561
+  cmTextTrimEnd(s);
562
+  return s;
563
+}
564
+
522 565
 
523 566
 cmChar_t* cmTextExpandS( cmChar_t* s, const cmChar_t* t, unsigned tn )
524 567
 { return cmVOC_Expand(s,strlen(s)+1,t,tn); }

+ 5
- 0
cmText.h 查看文件

@@ -161,6 +161,11 @@ extern "C" {
161 161
   // Remove the last n characters from s by inserting a '\0' at s[ strlen(s)-n ].
162 162
   void      cmTextClip(    cmChar_t* s, unsigned n );
163 163
 
164
+  // Trim white space from the begining/end/both of a string
165
+  cmChar_t* cmTextTrimBegin( cmChar_t* s );
166
+  cmChar_t* cmTextTrimEnd( cmChar_t* s );
167
+  cmChar_t* cmTextTrim( cmChar_t* );
168
+
164 169
   // Expand s by copying all bytes past t to t+tn.
165 170
   cmChar_t* cmTextExpandS( cmChar_t* s, const cmChar_t* t, unsigned tn );
166 171
 

正在加载...
取消
保存