Subversion Repositories SvarDOS

Rev

Rev 219 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 219 Rev 256
Line 1... Line 1...
1
/*
1
/*
2
 * Right trim any space, tab, cr or lf
2
 * trims any space, tab, cr or lf
3
 * Copyright (C) 2012-2016 Mateusz Viste
3
 * Copyright (C) 2012-2021 Mateusz Viste
4
 */
4
 */
5
 
5
 
6
#include "rtrim.h"
6
#include "trim.h"
7
 
-
 
8
void rtrim(char *str) {
-
 
9
  int x, realendofstr = 0;
-
 
10
  for (x = 0; str[x] != 0; x++) {
-
 
11
    switch (str[x]) {
-
 
12
      case ' ':
-
 
13
      case '\t':
-
 
14
      case '\r':
-
 
15
      case '\n':
-
 
16
        break;
-
 
17
      default:
-
 
18
        realendofstr = x + 1;
-
 
19
        break;
-
 
20
    }
-
 
21
  }
-
 
22
  str[realendofstr] = 0;
-
 
23
}
-
 
24
 
-
 
25
 
7
 
26
void trim(char *str) {
8
void trim(char *str) {
27
  int x, y, firstchar = -1, lastchar = -1;
9
  int x, y, firstchar = -1, lastchar = -1;
-
 
10
 
-
 
11
  /* find out first and last non-whitespace char */
28
  for (x = 0; str[x] != 0; x++) {
12
  for (x = 0; str[x] != 0; x++) {
29
    switch (str[x]) {
13
    switch (str[x]) {
30
      case ' ':
14
      case ' ':
31
      case '\t':
15
      case '\t':
32
      case '\n':
16
      case '\n':
Line 36... Line 20...
36
        if (firstchar < 0) firstchar = x;
20
        if (firstchar < 0) firstchar = x;
37
        lastchar = x;
21
        lastchar = x;
38
        break;
22
        break;
39
    }
23
    }
40
  }
24
  }
-
 
25
 
-
 
26
  /* right trim */
41
  str[lastchar + 1] = 0; /* right trim */
27
  str[lastchar + 1] = 0;
-
 
28
 
42
  if (firstchar > 0) { /* left trim (shift to the left ) */
29
  /* left trim (shift to the left ) */
-
 
30
  if (firstchar > 0) {
43
    y = 0;
31
    y = 0;
44
    for (x = firstchar; str[x] != 0; x++) str[y++] = str[x];
32
    for (x = firstchar; str[x] != 0; x++) str[y++] = str[x];
45
    str[y] = 0;
33
    str[y] = 0;
46
  }
34
  }
47
}
35
}