Subversion Repositories SvarDOS

Rev

Rev 614 | Details | Compare with Previous | Last modification | View Log | RSS feed

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