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