2 /* Copyright (C) 2007 Malek Cherier. All rights reserved. */
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.
8 /* Returns a copy of a string without leading whitespaces (ltrim), trailing whitespaces (rtrim),
9 * or both leading and trailing whitespaces (trim).
13 #include "xbt/sysdep.h" /* headers of these functions */
16 /** @brief Strip whitespace (or other characters) from the end of a string.
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 :
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.
28 * @param s The string to strip.
29 * @param char_list A string which contains the characters you want to strip.
31 * @return If the specified is NULL the function returns NULL. Otherwise the
32 * function returns the string with whitespace stripped from the end.
35 rtrim(char* s, const char* char_list)
38 const char* __char_list = " \t\n\r\x0B";
39 char white_char[256] = {1,0};
46 white_char[(unsigned char)*__char_list++] = 1;
50 white_char[(unsigned char)*char_list++] = 1;
57 while(white_char[(unsigned char)*cur] && (cur >= s))
64 /** @brief Strip whitespace (or other characters) from the beginning of a string.
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 :
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.
76 * @param s The string to strip.
77 * @param char_list A string which contains the characters you want to strip.
79 * @return If the specified is NULL the function returns NULL. Otherwise the
80 * function returns the string with whitespace stripped from the beginning.
83 ltrim( char* s, const char* char_list)
86 const char* __char_list = " \t\n\r\x0B";
87 char white_char[256] = {1,0};
94 white_char[(unsigned char)*__char_list++] = 1;
98 white_char[(unsigned char)*char_list++] = 1;
102 while(*cur && white_char[(unsigned char)*cur])
105 return strcpy(s,cur);
108 /** @brief Strip whitespace (or other characters) from the end and the begining of a string.
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 :
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.
120 * @param s The string to strip.
121 * @param char_list A string which contains the characters you want to strip.
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.
127 trim(char* s, const char* char_list ){
132 return ltrim(rtrim(s,char_list),char_list);