Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_HWPFILTER_SOURCE_MZSTRING_H
21 : #define INCLUDED_HWPFILTER_SOURCE_MZSTRING_H
22 :
23 : #ifdef HAVE_CONFIG_H
24 : # include "config.h"
25 : #endif
26 :
27 : /** @name MzString class
28 :
29 : It was supposed to be used instead of std::string.
30 :
31 : Notes for usage:
32 :
33 : When you declare an MzString, it is initially empty. There is no need to
34 : do things like #MzString a = "";#, especially not in constructors.
35 :
36 : If you want to use a default empty MzString as a parameter, use
37 :
38 : #void foo(MzString par = MzString()); // Correct#
39 :
40 : rather than
41 :
42 : #void foo(MzString par = ""); // WRONG!#
43 : #void foo(MzString par = 0); // WRONG!#
44 :
45 : (The last one is only wrong because some compilers can't handle it.)
46 :
47 : Methods that take an index as parameter all follow this rule: Valid indexes
48 : go from 0 to length()-1.
49 : \begin{tabular}{rl}
50 : Correct: & #foo.substr(0, length()-1);# \\
51 : Wrong: & #bar.substr(0, length());#
52 : \end{tabular}
53 :
54 : It is important that you declare MzStrings as const if possible, because
55 : some methods are much more efficient in const versions.
56 :
57 : If you want to check whether a string is empty, do
58 :
59 : #if (foo.empty()) something right#
60 :
61 : rather than something along the lines of
62 :
63 : #if (!foo) completely wrong#
64 :
65 : When you use the #.copy()# method, MzString calls "#new []#", so you have to
66 : release the memory with #delete[]#. Don't preallocate memory.
67 :
68 : When you want to copy an MzString, just do
69 :
70 : #MzString a, b = "String";#
71 : #a = b; // That's it!#
72 :
73 : not something like
74 :
75 : #MzString a, b = "String";#
76 : #a = b.copy();#
77 :
78 : The class automatically handles deep copying when required.
79 : */
80 :
81 : class MzString
82 : {
83 : public:
84 : MzString(); // Create an empty string
85 : // if len = 0, len becomes s.length)
86 : MzString(MzString const &s, int len = 0);
87 : ~MzString();
88 :
89 : int length() const;
90 : const char* c_str() const;
91 0 : operator char*() { return const_cast<char *>(c_str()); }
92 :
93 : // If it is not possible to use the constructor with an initial
94 : // allocation size, use the following member to set the size.
95 : bool resize(int len);
96 :
97 : // Assignment
98 : MzString &operator = (MzString &s);
99 : MzString &operator = (const char *s);
100 :
101 : // Appending
102 : MzString &operator += (char);
103 : MzString &operator += (const char *);
104 : MzString &operator += (MzString const &);
105 :
106 : MzString &operator << (const char *);
107 : MzString &operator << (char);
108 : MzString &operator << (unsigned char c) { return *this<<(char)c; }
109 : MzString &operator << (int);
110 : MzString &operator << (long);
111 : MzString &operator << (short i) { return *this<<(int)i; }
112 : MzString &operator << (MzString const &);
113 : /* MzString &operator << (MzString *s) { return *this<<*s; }
114 :
115 : // Removing
116 : char operator >> (char &c);
117 : */
118 : // Access to specific characters
119 : //char &operator [] (int n);
120 : char operator [] (int n);
121 : char last();
122 :
123 : // Comparison
124 : // Return:
125 : // 0 : 'this' is equal to 's'.
126 : // -1 : 'this' is less than 's'.
127 : // 1 : 'this' is greater than 's'.
128 : int compare(const char *s);
129 :
130 : // Searching for parts
131 : int find (char c);
132 : int find (char c, int pos);
133 : int find (char *);
134 : int find (char *, int pos);
135 : int rfind (char c);
136 : int rfind (char c, int pos);
137 :
138 : // Manipulation
139 : void replace(int, char c);
140 :
141 : void append (MzString const &s);
142 : void append (const char *s);
143 : void append (const char *s, int n);
144 :
145 : private:
146 : int Length; // Current Length
147 : int Allocated; // Total space allocated
148 : char *Data; // The actual contents
149 :
150 : // Allocate some space for the data.
151 : // Delete Data if it has been allocated.
152 : bool allocate(int len);
153 : };
154 :
155 0 : inline int MzString::length() const
156 : {
157 0 : return Length;
158 : }
159 :
160 :
161 0 : inline const char* MzString::c_str() const
162 : {
163 0 : if (Data)
164 : {
165 0 : Data[Length] = '\0'; // We always leave room for this.
166 0 : return Data;
167 : } else
168 0 : return "";
169 : }
170 :
171 :
172 :
173 : // Non friend, non member operators
174 :
175 : #endif // INCLUDED_HWPFILTER_SOURCE_MZSTRING_H
176 :
177 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|