]> cygwin.com Git - cygwin-apps/setup.git/blame - String++.h
2003-06-22 Benjamin Riefenstahl <Benjamin.Riefenstahl@epost.de>
[cygwin-apps/setup.git] / String++.h
CommitLineData
3c054baf
RC
1/*
2 * Copyright (c) 2002, Robert Collins.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by Robert Collins.
13 *
14 */
15
16// A String class to replace all the char * manipulation.
17
18#ifndef CINSTALL_STRING_H
19#define CINSTALL_STRING_H
20
21#include <stdarg.h>
45e01f23 22#include <sys/types.h>
6391823e 23#include <iosfwd>
b401ef47 24#include <string>
3c054baf
RC
25
26class io_stream;
27class String {
28 class _data;
29public:
30 // Static members first
3c054baf
RC
31 inline String();
32 inline String (String const &);
33 // We're notperformance bottlenecked.
34 String (const char *);
076654e7 35 String (int const);
318af134 36 String (std::string const &);
3c054baf
RC
37 inline String & operator = (String const &);
38 ~String();
39 // Up to the user to delete[] these.
40 char * cstr();
41 char * cstr() const; // may be less optimal
42 char const * cstr_oneuse() const; // only valid until the next mutator call
43 // pretends to be const !!
44 inline size_t size() const; // number of characters (!= storage size).
45 size_t find (char) const;
ad646f43 46 String substr (size_t start = 0, int len = -1) const;
3c054baf
RC
47 // operator == and != can be done if/when we have a 'casesensitive' flag to
48 // the constructors
49 // - means this sorts to the left of the parameter
50 int compare (String const &, size_t const = 0) const;
51 static int compare (String const &, String const &, size_t const = 0);
52 int casecompare (String const &, size_t const = 0) const;
53 static int casecompare (String const &, String const &, size_t const = 0);
54 static int casecompare (String const, String const);
55 String &append (String const &);
56 String &operator += (String const &);
57 String operator + (String const &) const;
58 String operator + (char const *) const;
59 bool operator == (String const &) const;
60 bool operator == (char const *) const;
405d7186
RC
61 struct caseless { bool operator () (String const &s1, String const &s2) const
62 {
63 return s1.casecompare (s2) < 0;
64 }};
864a5ec1 65 bool matches (String const &pattern) const;
3c054baf
RC
66
67private:
68 class _data {
69 public:
70 _data ();
71 _data (size_t);
72 _data (_data const &);
73 ~_data ();
74 unsigned count; //Invariant: all constructors set to 1;
75 // For now, char *, but can be TCHAR, or even UNICODE
76 // when time permits.
77 unsigned char *theString;
78 char *cstr; // cached/oneuse Cstr encoded version
79 size_t length;
80 } *theData; // Invariant, there is always an
81 static String absorb (unsigned char *, size_t);
82};
83
318af134
RC
84std::ostream &
85operator << (std::ostream &os, String const &theString);
6391823e 86
3c054baf
RC
87String::String() : theData (new _data) {}
88String::String(String const &aString) : theData (aString.theData)
89{
90 ++theData->count;
91}
92
93String &
94String::operator= (String const &aString)
95{
96 // Don't touch the order
97 ++aString.theData->count;
98 if (--theData->count == 0)
99 delete theData;
100 theData = aString.theData;
101 return *this;
102}
103
104size_t
105String::size() const
106{
107 return theData->length;
108}
109
110#endif // CINSTALL_STRING_H
This page took 0.040081 seconds and 5 git commands to generate.