Sfoglia il codice sorgente

cmText.h/c : Added cmTextNextRow(),cmTextMinIndent(),cmTextOutdent().

master
kevin 11 anni fa
parent
commit
e1a6257d74
2 ha cambiato i file con 102 aggiunte e 0 eliminazioni
  1. 88
    0
      cmText.c
  2. 14
    0
      cmText.h

+ 88
- 0
cmText.c Vedi File

@@ -875,3 +875,91 @@ cmChar_t* cmTextEatLeadingSpace( cmChar_t* s )
875 875
   return s;
876 876
 }
877 877
 
878
+cmChar_t*       cmTextNextRow(  cmChar_t* s )
879
+{
880
+  if( s == NULL)
881
+    return NULL;
882
+
883
+  for(; *s; ++s)
884
+    if( *s == '\n' )
885
+    {
886
+      ++s;
887
+      return *s==0 ? NULL : s;
888
+    }
889
+
890
+  return NULL;
891
+}
892
+
893
+const cmChar_t* cmTextNextRowC( const cmChar_t* s )
894
+{ return cmTextNextRow((cmChar_t*)s); }
895
+  
896
+unsigned  cmTextMinIndent( const cmChar_t* s )
897
+{
898
+  // leadFl=true if at beginning of row
899
+  bool     leadFl     = true;   
900
+  unsigned min_indent = INT_MAX;
901
+  unsigned indent     = 0;
902
+  for(; *s; ++s)
903
+  {
904
+    if( leadFl )
905
+    {
906
+      if( isspace(*s) && *s!='\n' )
907
+        indent += 1;
908
+      else
909
+      {
910
+        if( indent < min_indent )
911
+          min_indent = indent;
912
+
913
+        indent = 0;
914
+        leadFl = false;
915
+      }
916
+    }
917
+    else
918
+    {
919
+      if( *s == '\n' )
920
+        leadFl = true;
921
+    }
922
+      
923
+  }
924
+
925
+  return min_indent==INT_MAX ? 0 : min_indent;
926
+}
927
+
928
+cmChar_t*       cmTextOutdent( cmChar_t* s, unsigned outdent )
929
+{
930
+  // leadFl=true if at beginning of row
931
+  bool      leadFl = true;   
932
+  unsigned  indent = 0;
933
+  cmChar_t* cs     = s;
934
+  cmChar_t* s0     = s;
935
+
936
+  for(; *cs; ++cs)
937
+  {
938
+    if( leadFl )
939
+    {
940
+      if( isspace(*cs) && *cs!='\n' )
941
+        indent += 1;
942
+      else
943
+      {
944
+        unsigned n  = cmMin(outdent,indent);
945
+        cmTextShrinkS(s,s0,n);
946
+        cs         -= n;
947
+        
948
+        indent = 0;
949
+        leadFl = false;
950
+      }
951
+    }
952
+    else
953
+    {
954
+      if( *cs == '\n' )
955
+      {
956
+        leadFl = true;
957
+        s0     = cs + 1;
958
+      }
959
+    }
960
+      
961
+  }
962
+
963
+  return s;
964
+     
965
+}

+ 14
- 0
cmText.h Vedi File

@@ -249,6 +249,20 @@ extern "C" {
249 249
   // character.
250 250
   cmChar_t* cmTextEatLeadingSpace( cmChar_t* s );
251 251
 
252
+
253
+  // Return a pointer to the beginning of the next row
254
+  // or NULL if there is no next row.
255
+  cmChar_t*       cmTextNextRow(  cmChar_t* s );
256
+  const cmChar_t* cmTextNextRowC( const cmChar_t* s );
257
+  
258
+  // Return the minimum indent of all rows in s[].
259
+  unsigned        cmTextMinIndent( const cmChar_t* s );
260
+
261
+  // Outdent s[] by 'n'. 
262
+  // If a row is indented by less than 'n' then it is 
263
+  // then all leading white space is removed.
264
+  cmChar_t*       cmTextOutdent( cmChar_t* s, unsigned n );
265
+
252 266
   //)
253 267
   //}
254 268
 

Loading…
Annulla
Salva