Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
268002b0c124c774acef92d10f6065b0d7398097
[simgrid.git] / src / xbt / trim.c
1
2 /* Copyright (C) 2007 Malek Cherier. All rights reserved.                  */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. 
6  */
7
8 /* Returns a copy of a string without leading whitespaces (ltrim), trailing whitespaces (rtrim), 
9  * or both leading and trailing whitespaces (trim).
10  */
11   
12 #include "xbt/misc.h"
13 #include "xbt/sysdep.h" /* headers of these functions */
14 #include "portable.h"
15
16 /**  @brief Strip whitespace (or other characters) from the end of a string.
17  *
18  * The function rtrim() returns a string with whitespace stripped from the end of s. 
19  * By default (without the second parameter char_list), rtrim() will strip these characters :
20  *      
21  *      - " "           (ASCII 32       (0x20)) space. 
22  *      - "\t"          (ASCII 9        (0x09)) tab. 
23  *      - "\n"          (ASCII 10       (0x0A)) line feed. 
24  *      - "\r"          (ASCII 13       (0x0D)) carriage return. 
25  *      - "\0"          (ASCII 0        (0x00)) NULL. 
26  *      - "\x0B"        (ASCII 11       (0x0B)) vertical tab. 
27  *
28  * @param s The string to strip.
29  * @param char_list A string which contains the characters you want to strip.
30  *
31  * @return If the specified is NULL the function returns NULL. Otherwise the
32  * function returns the string with whitespace stripped from the end.
33  */
34 char*
35 rtrim(char* s, const char* char_list)
36 {
37         char* cur = s;
38         const char* __char_list = " \t\n\r\x0B";
39         char white_char[256] = {1,0};
40         
41         if(!s)
42                 return NULL;
43
44         if(!char_list){
45                 while(*__char_list) {
46                         white_char[(unsigned char)*__char_list++] = 1;
47                 }
48         }else{
49                 while(*char_list) {
50                         white_char[(unsigned char)*char_list++] = 1;
51                 }
52         }
53
54         while(*cur)
55                 ++cur;
56
57         while(white_char[(unsigned char)*cur] && (cur >= s))
58                 --cur;
59
60         *++cur = '\0';
61         return s;
62 }
63
64 /**  @brief Strip whitespace (or other characters) from the beginning of a string.
65  *
66  * The function ltrim() returns a string with whitespace stripped from the beginning of s. 
67  * By default (without the second parameter char_list), ltrim() will strip these characters :
68  *      
69  *      - " "           (ASCII 32       (0x20)) space. 
70  *      - "\t"          (ASCII 9        (0x09)) tab. 
71  *      - "\n"          (ASCII 10       (0x0A)) line feed. 
72  *      - "\r"          (ASCII 13       (0x0D)) carriage return. 
73  *      - "\0"          (ASCII 0        (0x00)) NULL. 
74  *      - "\x0B"        (ASCII 11       (0x0B)) vertical tab. 
75  *
76  * @param s The string to strip.
77  * @param char_list A string which contains the characters you want to strip.
78  *
79  * @return If the specified is NULL the function returns NULL. Otherwise the
80  * function returns the string with whitespace stripped from the beginning.
81  */
82 char*
83 ltrim( char* s, const char* char_list)
84 {
85         char* cur = s;
86         const char* __char_list = " \t\n\r\x0B";
87         char white_char[256] = {1,0};
88         
89         if(!s)
90                 return NULL;
91
92         if(!char_list){
93                 while(*__char_list) {
94                         white_char[(unsigned char)*__char_list++] = 1;
95                 }
96         }else{
97                 while(*char_list) {
98                         white_char[(unsigned char)*char_list++] = 1;
99                 }
100         }
101
102         while(*cur && white_char[(unsigned char)*cur])
103                 ++cur;
104
105         return strcpy(s,cur);
106 }
107
108 /**  @brief Strip whitespace (or other characters) from the end and the begining of a string.
109  *
110  * The function trim() returns a string with whitespace stripped from the end and the begining of s. 
111  * By default (without the second parameter char_list), trim() will strip these characters :
112  *      
113  *      - " "           (ASCII 32       (0x20)) space. 
114  *      - "\t"          (ASCII 9        (0x09)) tab. 
115  *      - "\n"          (ASCII 10       (0x0A)) line feed. 
116  *      - "\r"          (ASCII 13       (0x0D)) carriage return. 
117  *      - "\0"          (ASCII 0        (0x00)) NULL. 
118  *      - "\x0B"        (ASCII 11       (0x0B)) vertical tab. 
119  *
120  * @param s The string to strip.
121  * @param char_list A string which contains the characters you want to strip.
122  *
123  * @return If the specified is NULL the function returns NULL. Otherwise the
124  * function returns the string with whitespace stripped from the end and the begining.
125  */
126 char* 
127 trim(char* s, const char* char_list ){
128         
129         if(!s)
130                 return NULL;
131                 
132     return ltrim(rtrim(s,char_list),char_list);
133 }
134