aboutsummaryrefslogtreecommitdiff
path: root/libxsd/xsd/cxx/ro-string.txx
blob: 1685a4608fd3e79adcc4f821fc5d8ca3f235521d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// file      : xsd/cxx/ro-string.txx
// copyright : Copyright (c) 2005-2017 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

namespace xsd
{
  namespace cxx
  {
    template <typename C>
    typename ro_string<C>::size_type ro_string<C>::
    find (C c, size_type pos) const
    {
      size_type r (npos);

      if (pos < size_)
      {
        if (const C* p = traits_type::find(data_ + pos, size_ - pos, c))
          r = p - data_;
      }

      return r;
    }

    template<typename C>
    typename ro_string<C>::size_type
    trim_left (ro_string<C>& s)
    {
      typename ro_string<C>::size_type size (s.size ());

      if (size != 0)
      {
        const C* f (s.data ());
        const C* l (f + size);
        const C* of (f);

        while (f < l &&
               (*f == C (0x20) || *f == C (0x0A) ||
                *f == C (0x0D) || *f == C (0x09)))
          ++f;

        if (f != of)
        {
          size = f <= l ? l - f : 0;
          s.assign ((f <= l ? f : 0), size);
        }
      }

      return size;
    }

    template<typename C>
    typename ro_string<C>::size_type
    trim_right (ro_string<C>& s)
    {
      typename ro_string<C>::size_type size (s.size ());

      if (size != 0)
      {
        const C* f (s.data ());
        const C* l (f + size - 1);
        const C* ol (l);

        while (l > f &&
               (*l == C (0x20) || *l == C (0x0A) ||
                *l == C (0x0D) || *l == C (0x09)))
          --l;

        if (l != ol)
        {
          size = f <= l ? l - f + 1 : 0;
          s.assign ((f <= l ? f : 0), size);
        }
      }

      return size;
    }

    template<typename C>
    typename ro_string<C>::size_type
    trim (ro_string<C>& s)
    {
      typename ro_string<C>::size_type size (s.size ());

      if (size != 0)
      {
        const C* f (s.data ());
        const C* l (f + size);

        const C* of (f);

        while (f < l &&
               (*f == C (0x20) || *f == C (0x0A) ||
                *f == C (0x0D) || *f == C (0x09)))
          ++f;

        --l;

        const C* ol (l);

        while (l > f &&
               (*l == C (0x20) || *l == C (0x0A) ||
                *l == C (0x0D) || *l == C (0x09)))
          --l;

        if (f != of || l != ol)
        {
          size = f <= l ? l - f + 1 : 0;
          s.assign ((f <= l ? f : 0), size);
        }
      }

      return size;
    }

    template<typename C>
    std::basic_string<C>
    trim (const std::basic_string<C>& s)
    {
      ro_string<C> tmp (s);
      typename ro_string<C>::size_type size (tmp.size ());
      trim (tmp);

      // If we didn't change the string then return the original to help
      // avoid copying for smart (ref counted) string implementations.
      //
      if (size == tmp.size ())
        return s;
      else
        return tmp;
    }
  }
}