root/win32/xsd-3.1.0-i686/libxsd/xsd/cxx/ro-string.txx @ 111

Revision 111, 2.9 kB (checked in by mido, 16 years ago)

pridana knihovna XSD (a jeji chlebodarkyne XERCES), v ramci Win32 zprovoznen priklad tests/test_xsd_hello.cxx

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