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"
14 #include "xbt/str.h" /* headers of these functions */
17 /** @brief Strip whitespace (or other characters) from the end of a string.
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 :
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.
29 * @param s The string to strip.
30 * @param char_list A string which contains the characters you want to strip.
32 * @return If the specified is NULL the function returns NULL. Otherwise the
33 * function returns the string with whitespace stripped from the end.
36 rtrim(char* s, const char* char_list)
39 const char* __char_list = " \t\n\r\x0B";
40 char white_char[256] = {1,0};
47 white_char[(unsigned char)*__char_list++] = 1;
51 white_char[(unsigned char)*char_list++] = 1;
58 while(white_char[(unsigned char)*cur] && (cur >= s))
65 /** @brief Strip whitespace (or other characters) from the beginning of a string.
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 :
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.
77 * @param s The string to strip.
78 * @param char_list A string which contains the characters you want to strip.
80 * @return If the specified is NULL the function returns NULL. Otherwise the
81 * function returns the string with whitespace stripped from the beginning.
84 ltrim( char* s, const char* char_list)
87 const char* __char_list = " \t\n\r\x0B";
88 char white_char[256] = {1,0};
95 white_char[(unsigned char)*__char_list++] = 1;
99 white_char[(unsigned char)*char_list++] = 1;
103 while(*cur && white_char[(unsigned char)*cur])
106 return strcpy(s,cur);
109 /** @brief Strip whitespace (or other characters) from the end and the begining of a string.
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 :
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.
121 * @param s The string to strip.
122 * @param char_list A string which contains the characters you want to strip.
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.
128 trim(char* s, const char* char_list ){
133 return ltrim(rtrim(s,char_list),char_list);