Subversion Repositories SvarDOS

Rev

Rev 256 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
2
 * Right trim any space, tab, cr or lf
3
 * Copyright (C) 2012-2016 Mateusz Viste
4
 */
5
 
6
#include "rtrim.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
 
26
void trim(char *str) {
27
  int x, y, firstchar = -1, lastchar = -1;
28
  for (x = 0; str[x] != 0; x++) {
29
    switch (str[x]) {
30
      case ' ':
31
      case '\t':
32
      case '\n':
33
      case '\r':
34
        break;
35
      default:
36
        if (firstchar < 0) firstchar = x;
37
        lastchar = x;
38
        break;
39
    }
40
  }
41
  str[lastchar + 1] = 0; /* right trim */
42
  if (firstchar > 0) { /* left trim (shift to the left ) */
43
    y = 0;
44
    for (x = firstchar; str[x] != 0; x++) str[y++] = str[x];
45
    str[y] = 0;
46
  }
47
}