Subversion Repositories SvarDOS

Rev

Rev 269 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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