summaryrefslogtreecommitdiff
path: root/lib/mystring/mystring.h
blob: 85a63ccd253760aa11355be191c824962e6300d9 (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
/* $Id: mystring.h 635 2005-11-02 17:37:50Z bruce $ */
// Copyright (C) 2016 Bruce Guenter <bruce@untroubled.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#ifndef MYSTRING__H__
#define MYSTRING__H__

#include <sys/types.h>
#include "mystring/rep.h"

class mystringjoin;
class mystring
{
  friend class mystringtmp;
  friend class mystringjoin;
private:
  mystringrep* rep;

protected:
  void dupnil();
  void dup(const char*, size_t);
  void dup(const char*);
  void assign(const char*);
  void assign(const char*, size_t);
public:
  static const mystring NUL;
  
  mystring() { dupnil(); }
  mystring(const char* s) { dup(s); }
  mystring(const mystring& s) { dup(s.rep->buf, s.rep->length); }
  mystring(const char* str, size_t len) { dup(str, len); }
  mystring(const mystringjoin&);
  ~mystring();

  const char* c_str() const { return rep->buf; }

  bool operator!() const { return empty(); }
  
  char operator[](size_t i) const { return rep->buf[i]; }
  
  size_t length() const { return rep->length; }

  bool empty() const { return rep->length == 0; }
  
  int operator!=(const char* in) const;
  int operator!=(const mystring& in) const;
  bool operator==(const char* in) const
    {
      return !operator!=(in);
    }
  bool operator==(const mystring& in) const
    {
      return !operator!=(in);
    }

  void operator=(const char* in) { assign(in); }
  void operator=(const mystring& in) { assign(in.rep->buf, in.rep->length); }
  void operator=(const mystringjoin& in);

  mystring subst(char from, char to) const;
  
  mystring lower() const;
  mystring upper() const;

  int find_first(char, size_t = 0) const;
  int find_first_of(const mystring&, size_t = 0) const;
  int find_first_of(const char*, size_t = 0) const;
  int find_first_of(const char*, size_t, size_t) const;

  int find_last(char, size_t = (size_t)-1) const;
  int find_last_of(const mystring&, size_t = (size_t)-1) const;
  int find_last_of(const char*, size_t = 0) const;
  int find_last_of(const char*, size_t, size_t) const;

  mystring left(size_t) const;
  mystring right(size_t) const;
  mystring sub(size_t, size_t) const;

  mystring lstrip() const;
  mystring rstrip() const;
  mystring strip() const;

  unsigned count(char ch) const;
  
  void append(const char*);
  void append(const char*, size_t);

  void operator+=(const mystring& str) {append(str.rep->buf, str.rep->length);}
  void operator+=(const char* str) { append(str); }
  void operator+=(char ch)
    {
      char str[2] = { ch, 0 };
      append(str, 1);
    }
};

#ifndef MYSTRING_TRACE
inline mystring::~mystring()
{
  rep->detach();
}
#endif

#include "mystring/iter.h"
#include "mystring/join.h"

class fdobuf;
fdobuf& operator<<(fdobuf& out, const mystring& str);

//istream& operator>>(istream& in, mystring& str);

typedef mystring string;

#endif