Line data Source code
1 : // file : xsd/cxx/ro-string.txx
2 : // copyright : Copyright (c) 2005-2017 Code Synthesis Tools CC
3 : // license : GNU GPL v2 + exceptions; see accompanying LICENSE file
4 :
5 : namespace xsd
6 : {
7 : namespace cxx
8 : {
9 : template <typename C>
10 : typename ro_string<C>::size_type ro_string<C>::
11 : find (C c, size_type pos) const
12 : {
13 : size_type r (npos);
14 :
15 : if (pos < size_)
16 : {
17 : if (const C* p = traits_type::find(data_ + pos, size_ - pos, c))
18 : r = p - data_;
19 : }
20 :
21 : return r;
22 : }
23 :
24 : template<typename C>
25 : typename ro_string<C>::size_type
26 : trim_left (ro_string<C>& s)
27 : {
28 : typename ro_string<C>::size_type size (s.size ());
29 :
30 : if (size != 0)
31 : {
32 : const C* f (s.data ());
33 : const C* l (f + size);
34 : const C* of (f);
35 :
36 : while (f < l &&
37 : (*f == C (0x20) || *f == C (0x0A) ||
38 : *f == C (0x0D) || *f == C (0x09)))
39 : ++f;
40 :
41 : if (f != of)
42 : {
43 : size = f <= l ? l - f : 0;
44 : s.assign ((f <= l ? f : 0), size);
45 : }
46 : }
47 :
48 : return size;
49 : }
50 :
51 : template<typename C>
52 : typename ro_string<C>::size_type
53 : trim_right (ro_string<C>& s)
54 : {
55 : typename ro_string<C>::size_type size (s.size ());
56 :
57 : if (size != 0)
58 : {
59 : const C* f (s.data ());
60 : const C* l (f + size - 1);
61 : const C* ol (l);
62 :
63 : while (l > f &&
64 : (*l == C (0x20) || *l == C (0x0A) ||
65 : *l == C (0x0D) || *l == C (0x09)))
66 : --l;
67 :
68 : if (l != ol)
69 : {
70 : size = f <= l ? l - f + 1 : 0;
71 : s.assign ((f <= l ? f : 0), size);
72 : }
73 : }
74 :
75 : return size;
76 : }
77 :
78 : template<typename C>
79 : typename ro_string<C>::size_type
80 0 : trim (ro_string<C>& s)
81 : {
82 0 : typename ro_string<C>::size_type size (s.size ());
83 :
84 0 : if (size != 0)
85 : {
86 0 : const C* f (s.data ());
87 0 : const C* l (f + size);
88 :
89 0 : const C* of (f);
90 :
91 0 : while (f < l &&
92 0 : (*f == C (0x20) || *f == C (0x0A) ||
93 0 : *f == C (0x0D) || *f == C (0x09)))
94 0 : ++f;
95 :
96 0 : --l;
97 :
98 0 : const C* ol (l);
99 :
100 0 : while (l > f &&
101 0 : (*l == C (0x20) || *l == C (0x0A) ||
102 0 : *l == C (0x0D) || *l == C (0x09)))
103 0 : --l;
104 :
105 0 : if (f != of || l != ol)
106 : {
107 0 : size = f <= l ? l - f + 1 : 0;
108 0 : s.assign ((f <= l ? f : 0), size);
109 : }
110 : }
111 :
112 0 : return size;
113 : }
114 :
115 : template<typename C>
116 : std::basic_string<C>
117 0 : trim (const std::basic_string<C>& s)
118 : {
119 0 : ro_string<C> tmp (s);
120 0 : typename ro_string<C>::size_type size (tmp.size ());
121 0 : trim (tmp);
122 :
123 : // If we didn't change the string then return the original to help
124 : // avoid copying for smart (ref counted) string implementations.
125 : //
126 0 : if (size == tmp.size ())
127 0 : return s;
128 : else
129 0 : return tmp;
130 : }
131 : }
132 : }
|