]> cygwin.com Git - cygwin-apps/setup.git/blame - String++.h
2002-05-04 Robert Collins <rbtcollins@hotmail.com>
[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>
3c054baf
RC
24
25class io_stream;
26class String {
27 class _data;
28public:
29 // Static members first
3c054baf
RC
30 inline String();
31 inline String (String const &);
32 // We're notperformance bottlenecked.
33 String (const char *);
076654e7 34 String (int const);
3c054baf
RC
35 inline String & operator = (String const &);
36 ~String();
37 // Up to the user to delete[] these.
38 char * cstr();
39 char * cstr() const; // may be less optimal
40 char const * cstr_oneuse() const; // only valid until the next mutator call
41 // pretends to be const !!
42 inline size_t size() const; // number of characters (!= storage size).
43 size_t find (char) const;
44 // operator == and != can be done if/when we have a 'casesensitive' flag to
45 // the constructors
46 // - means this sorts to the left of the parameter
47 int compare (String const &, size_t const = 0) const;
48 static int compare (String const &, String const &, size_t const = 0);
49 int casecompare (String const &, size_t const = 0) const;
50 static int casecompare (String const &, String const &, size_t const = 0);
51 static int casecompare (String const, String const);
52 String &append (String const &);
53 String &operator += (String const &);
54 String operator + (String const &) const;
55 String operator + (char const *) const;
56 bool operator == (String const &) const;
57 bool operator == (char const *) const;
58
59private:
60 class _data {
61 public:
62 _data ();
63 _data (size_t);
64 _data (_data const &);
65 ~_data ();
66 unsigned count; //Invariant: all constructors set to 1;
67 // For now, char *, but can be TCHAR, or even UNICODE
68 // when time permits.
69 unsigned char *theString;
70 char *cstr; // cached/oneuse Cstr encoded version
71 size_t length;
72 } *theData; // Invariant, there is always an
73 static String absorb (unsigned char *, size_t);
74};
75
6391823e
RC
76ostream &
77operator << (ostream &os, String const &theString);
78
3c054baf
RC
79String::String() : theData (new _data) {}
80String::String(String const &aString) : theData (aString.theData)
81{
82 ++theData->count;
83}
84
85String &
86String::operator= (String const &aString)
87{
88 // Don't touch the order
89 ++aString.theData->count;
90 if (--theData->count == 0)
91 delete theData;
92 theData = aString.theData;
93 return *this;
94}
95
96size_t
97String::size() const
98{
99 return theData->length;
100}
101
102#endif // CINSTALL_STRING_H
This page took 0.030548 seconds and 5 git commands to generate.