1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "stdafx.h"
17 
18 #include <cstring>
19 
20 #include "ascii_util.h"
21 
22 namespace zorba {
23 namespace ascii {
24 
25 ///////////////////////////////////////////////////////////////////////////////
26 
is_whitespace(char const * s)27 bool is_whitespace( char const *s ) {
28   for ( ; *s; ++s ) {
29     if ( !is_space( *s ) )
30       return false;
31   }
32   return true;
33 }
34 
remove_chars(char * s,size_type s_len,char const * chars)35 size_type remove_chars( char *s, size_type s_len, char const *chars ) {
36   char *end = s + s_len;
37   char *c;
38 
39   // remove trailing chars first
40   for ( c = end - 1; c >= s; --c )
41     if ( !std::strchr( chars, *c ) ) {
42       end = c + 1;
43       break;
44     }
45   if ( c < s )                          // it was all chars
46     return 0;
47 
48   // remove all other chars
49   char *first_char = nullptr;
50   for ( c = s; c < end; ++c ) {
51     if ( std::strchr( chars, *c ) ) {
52       if ( !first_char )
53         first_char = c;
54     } else {
55       if ( first_char ) {
56         std::memmove( first_char, c, end - c );
57         end -= c - first_char;
58         c = first_char;
59         first_char = nullptr;
60       }
61     }
62   }
63 
64   return end - s;
65 }
66 
trim_start(char const * s,char const * chars)67 char const* trim_start( char const *s, char const *chars ) {
68   for ( ; *s; ++s ) {
69     if ( !std::strchr( chars, *s ) )
70       break;
71   }
72   return s;
73 }
74 
trim_start(char const * s,size_type s_len,char const * chars)75 char const* trim_start( char const *s, size_type s_len, char const *chars ) {
76   for ( ; s_len-- > 0; ++s ) {
77     if ( !std::strchr( chars, *s ) )
78       break;
79   }
80   return s;
81 }
82 
trim_end(char const * s,size_type s_len,char const * chars)83 size_type trim_end( char const *s, size_type s_len, char const *chars ) {
84   s += s_len - 1;
85   for ( ; s_len > 0; --s, --s_len ) {
86     if ( !std::strchr( chars, *s ) )
87       break;
88   }
89   return s_len;
90 }
91 
92 ///////////////////////////////////////////////////////////////////////////////
93 
94 } // namespace ascii
95 } // namespace zorba
96 /* vim:set et sw=2 ts=2: */
97