Main Page Class Hierarchy Alphabetical List Compound List Examples

strutils.h
1/***************************************************************************
2 copyright : (C) 2002-2008 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: strutils.h,v 1.10 2008-10-07 11:06:26 tat Exp $
6 ***************************************************************************/
7#ifndef _MIMETIC_STRINGUTILS_H_
8#define _MIMETIC_STRINGUTILS_H_
9#include <string>
10#include <cstring>
11#include <iostream>
12#include <algorithm>
13#include <cstring>
14
15 namespace mimetic
16{
17
18 extern const std::string nullstring;
19
20 struct ichar_traits : public std::char_traits<char>
21{
22 static bool eq (const char_type & c1, const char_type& c2)
23 { return (toupper(c1) == toupper(c2)); }
24 static bool ne (const char_type& c1, const char_type& c2)
25 { return (toupper(c1) != toupper(c2)); }
26 static bool lt (const char_type& c1, const char_type& c2)
27 { return (toupper(c1) < toupper(c2)); }
28 static int compare (const char_type* s1, const char_type* s2, size_t n)
29 {
30 for(size_t i=0; i < n; ++i)
31 if(toupper(s1[i]) != toupper(s2[i]))
32 return (toupper(s1[i]) < toupper(s2[i])) ?-1: 1;
33 return 0;
34 }
35 static const char* find( const char* s, int n, char a )
36 {
37 while( n-- > 0 && tolower(*s) != tolower(a) )
38 ++s;
39 return s;
40 }
41};
42
43//typedef std::istring <char, ichar_traits> istring;
44 using std::string;
45
46 struct istring: public string
47{
48 istring()
49 {}
50 //typedef std::string::allocator_type allocator_type;
51 istring(const std::string& right)
52 : string(right)
53 {}
54 explicit istring(const allocator_type& al)
55 : string(al)
56 {}
57 istring(const istring& right)
58 : string(right)
59 {}
60 istring(const istring& right, size_type roff, size_type count = npos)
61 : string(right, roff, count)
62 {}
63 istring(const istring& right, size_type roff, size_type count,
64 const allocator_type& al)
65 : string(right, roff, count, al)
66 {}
67 istring(const value_type *ptr, size_type count)
68 : string(ptr, count)
69 {}
70 istring(const value_type *ptr, size_type count,const allocator_type& al)
71 : string(ptr, count, al)
72 {}
73 istring(const value_type *ptr)
74 : string(ptr)
75 {}
76 istring(const value_type *ptr,const allocator_type& al)
77 : string(ptr, al)
78 {}
79 istring(size_type count, value_type ch)
80 : string(count,ch)
81 {}
82 istring(size_type count, value_type ch,const allocator_type& al)
83 : string(count,ch,al)
84 {}
85 template <class InIt>
86 istring(InIt first, InIt last)
87 : string(first, last)
88 {}
89 template <class InIt>
90 istring(InIt first, InIt last,const allocator_type& al)
91 : string(first, last, al)
92 {}
93};
94
95
96 inline bool operator==(const istring& is, const std::string& s)
97{
98 return (0 == ichar_traits::compare(is.c_str(),s.c_str(),
99 std::max(is.length(),s.length())) );
100}
101
102 inline bool operator!=(const istring& is, const std::string& s)
103{
104 return (0 != ichar_traits::compare(is.c_str(),s.c_str(),
105 std::max(is.length(),s.length())) );
106}
107
108 inline bool operator!=(const istring& is, const char* str)
109{
110 return (0 != ichar_traits::compare(is.c_str(),str,
111 std::max(is.length(),::strlen(str))) );
112}
113
114 inline bool operator==(const istring& is, const char* str)
115{
116 return (0 == ichar_traits::compare(is.c_str(),str,
117 std::max(is.length(),::strlen(str))) );
118}
119
120 inline std::string dquoted(const std::string& s)
121{
122 return "\"" + s + "\"";
123}
124
125 inline std::string parenthed(const std::string& s)
126{
127 return "(" + s + ")";
128}
129
130/// removes double quotes
131 inline std::string remove_dquote(const std::string& s)
132{
133 int len = s.length();
134 if( len < 2)
135 return s;
136 if(s[0] == '"' && s[len-1] == '"')
137 return std::string(s, 1, len-2);
138 return s;
139}
140
141/**
142 * returns the \e canonical representation of \p s (see RFC822)
143 * if \p no_ws is true removes all blanks from the resulting string
144 */
145std::string canonical(const std::string& s, bool no_ws = false);
146
147/// removes leading and trailing blanks
148 inline std::string remove_external_blanks(const std::string& in)
149{
150 if(!in.length())
151 return in;
152 std::string s = in;
153 int beg = 0, end = s.length();
154 for(; beg < end; ++beg)
155 if(s[beg] != ' ' && s[beg] != '\t')
156 break;
157 end = s.length() - 1;
158 for(; end > beg; --end)
159 if(s[end] != ' ' && s[end] != '\t')
160 break;
161 s.assign(std::string(s, beg, end - beg + 1));
162 return s;
163}
164
165}
166
167#endif
168
mimetic
Definition body.h:18
mimetic::canonical
std::string canonical(const std::string &s, bool no_ws=false)
mimetic::remove_dquote
std::string remove_dquote(const std::string &s)
removes double quotes
Definition strutils.h:131
mimetic::remove_external_blanks
std::string remove_external_blanks(const std::string &in)
removes leading and trailing blanks
Definition strutils.h:148