aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/ro-string.cxx
blob: 9b099fb2a6a9501620e5fc9c7d82ad18c2deb6f1 (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
// file      : xsde/cxx/ro-string.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <xsde/cxx/ro-string.hxx>

#ifdef XSDE_IOSTREAM
#  include <iostream>
#endif

namespace xsde
{
  namespace cxx
  {
    // ro_string
    const size_t ro_string::npos = ~(size_t (0));
  
    // operator <<
    //
#ifdef XSDE_IOSTREAM
    std::ostream&
    operator<< (std::ostream& os, const ro_string& str)
    {
      if (str.size () != 0)
        os.write (str.data (), str.size ());

      return os;
    }
#endif

    size_t
    trim_left (ro_string& s)
    {
      size_t size = s.size ();

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

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

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

      return size;
    }

    size_t
    trim_right (ro_string& s)
    {
      size_t size = s.size ();

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

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

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

      return size;
    }

    size_t
    trim (ro_string& s)
    {
      size_t size = s.size ();

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

        const char* of = f;

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

        --l;

        const char* ol = l;

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

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

      return size;
    }
  }
}