File: stringTools.ec

package info (click to toggle)
ecere-sdk 0.44.15-1
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 97,712 kB
  • ctags: 54,695
  • sloc: ansic: 593,042; makefile: 12,250; yacc: 4,955; lex: 707; objc: 259; python: 252; xml: 102
file content (87 lines) | stat: -rw-r--r-- 2,172 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifdef ECERE_STATIC
public import static "ecere"
#else
public import "ecere"
#endif

public enum Trim { no, left = 1, right = 2, ends = 3, middle = 4, all = 7 };

void TrimChars(const char * string, char * output, const char * chars, Trim trim, bool squash, char alt)
{
   const char * s = string;
   char * o = output;
   char ch;
   bool keepChar = (trim & left) != left;
   bool keepMiddleChars = (trim & middle) != middle;
   for(; (ch = *s); s++)
   {
      if(strchr(chars, ch))
      {
         if(keepChar)
         {
            *o++ = alt;
            if(squash)
               keepChar = false;
         }
      }
      else
      {
         if(!keepChar && keepMiddleChars)
            keepChar = true;
         *o++ = ch;
      }
   }
   if(keepMiddleChars && (trim & right) == right && o > output && *(o-1) == alt)
      o--;
   *o = '\0';
}

char * TrimCharsCopy(const char * string, const char * chars, Trim trim, bool squash, char alt)
{
   int len = strlen(string);
   char * output = new char[len+1];
   TrimChars(string, output, chars, trim, squash, alt);
   len = strlen(output);
   output = renew output char[len+1];
   return output;
}

void TrimTestChars(const char * string, char * output, int (*CharTest)(char), Trim trim, bool squash, char alt)
{
   const char * s = string;
   char * o = output;
   char ch;
   bool keepChar = (trim & left) != left;
   bool keepMiddleChars = (trim & middle) != middle;
   for(; (ch = *s); s++)
   {
      if(CharTest(ch))
      {
         if(keepChar)
         {
            *o++ = alt;
            if(squash)
               keepChar = false;
         }
      }
      else
      {
         if(!keepChar && keepMiddleChars)
            keepChar = true;
         *o++ = ch;
      }
   }
   if(keepMiddleChars && (trim & right) == right && o > output && *(o-1) == alt)
      o--;
   *o = '\0';
}

char * TrimTestCharsCopy(const char * string, int (*CharTest)(char), Trim trim, bool squash, char alt)
{
   int len = strlen(string);
   char * output = new char[len+1];
   TrimTestChars(string, output, CharTest, trim, squash, alt);
   len = strlen(output);
   output = renew output char[len+1];
   return output;
}