Logo AND Algorithmique Numérique Distribuée

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