ecere/Desktop3D: (WIP) Attempts to fix full screen mode
[sdk] / extras / stringTools.ec
1 #ifdef ECERE_STATIC
2 public import static "ecere"
3 #else
4 public import "ecere"
5 #endif
6
7 public enum Trim { no, left = 1, right = 2, ends = 3, middle = 4, all = 7 };
8
9 void TrimChars(char * string, char * output, char * chars, Trim trim, bool squash, char alt)
10 {
11    char * s = string, * o = output;
12    char ch;
13    bool keepChar = (trim & left) != left;
14    bool keepMiddleChars = (trim & middle) != middle;
15    for(; (ch = *s); s++)
16    {
17       if(strchr(chars, ch))
18       {
19          if(keepChar)
20          {
21             *o++ = alt;
22             if(squash)
23                keepChar = false;
24          }
25       }
26       else
27       {
28          if(!keepChar && keepMiddleChars)
29             keepChar = true;
30          *o++ = ch;
31       }
32    }
33    if(keepMiddleChars && (trim & right) == right && o > output && *(o-1) == alt)
34       o--;
35    *o = '\0';
36 }
37
38 char * TrimCharsCopy(char * string, char * chars, Trim trim, bool squash, char alt)
39 {
40    int len = strlen(string);
41    char * output = new char[len+1];
42    TrimChars(string, output, chars, trim, squash, alt);
43    len = strlen(output);
44    output = renew output char[len+1];
45    return output;
46 }
47
48 void TrimTestChars(char * string, char * output, int (*CharTest)(char), Trim trim, bool squash, char alt)
49 {
50    char * s = string, * o = output;
51    char ch;
52    bool keepChar = (trim & left) != left;
53    bool keepMiddleChars = (trim & middle) != middle;
54    for(; (ch = *s); s++)
55    {
56       if(CharTest(ch))
57       {
58          if(keepChar)
59          {
60             *o++ = alt;
61             if(squash)
62                keepChar = false;
63          }
64       }
65       else
66       {
67          if(!keepChar && keepMiddleChars)
68             keepChar = true;
69          *o++ = ch;
70       }
71    }
72    if(keepMiddleChars && (trim & right) == right && o > output && *(o-1) == alt)
73       o--;
74    *o = '\0';
75 }
76
77 char * TrimTestCharsCopy(char * string, int (*CharTest)(char), Trim trim, bool squash, char alt)
78 {
79    int len = strlen(string);
80    char * output = new char[len+1];
81    TrimTestChars(string, output, CharTest, trim, squash, alt);
82    len = strlen(output);
83    output = renew output char[len+1];
84    return output;
85 }