git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19731 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2007-01-06 18:06:38 +00:00
parent 955dc88f0c
commit 43cca04a9a
2 changed files with 420 additions and 506 deletions
+224 -303
View File
@@ -1,360 +1,281 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2003, OpenBeOS
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: String.h
// Author(s): Stefano Ceccherini ([email protected])
// Description: String class supporting common string operations.
//------------------------------------------------------------------------------
/*
* Copyright 2001-2007, Haiku Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef __BSTRING__
#define __BSTRING__
#include <BeBuild.h>
#include <SupportDefs.h>
#include <string.h>
class BString {
public:
BString();
BString(const char *);
BString(const BString &);
BString(const char *, int32 maxLength);
~BString();
/*---- Access --------------------------------------------------------------*/
const char *String() const;
/* returns null-terminated string */
public:
BString();
BString(const char* string);
BString(const BString& string);
BString(const char* string, int32 maxLength);
~BString();
int32 Length() const;
/* length of corresponding string */
// Access
const char* String() const;
int32 CountChars() const;
/* returns number of UTF8 characters in string */
/*---- Assignment ----------------------------------------------------------*/
BString &operator=(const BString &);
BString &operator=(const char *);
BString &operator=(char);
BString &SetTo(const char *);
BString &SetTo(const char *, int32 length);
int32 Length() const;
// length of corresponding string
int32 CountChars() const;
// returns number of UTF8 characters in string
BString &SetTo(const BString &from);
BString &Adopt(BString &from);
/* leaves <from> empty, avoiding a copy */
BString &SetTo(const BString &, int32 length);
BString &Adopt(BString &from, int32 length);
/* leaves <from> empty, avoiding a copy */
BString &SetTo(char, int32 count);
// Assignment
BString& operator=(const BString& string);
BString& operator=(const char* string);
BString& operator=(char c);
/*---- Substring copying ---------------------------------------------------*/
BString &CopyInto(BString &into, int32 fromOffset,
BString& SetTo(const char* string);
BString& SetTo(const char* string, int32 maxLength);
BString& SetTo(const BString& string);
BString& Adopt(BString& from);
BString& SetTo(const BString& string, int32 maxLength);
BString& Adopt(BString& from, int32 maxLength);
BString& SetTo(char c, int32 count);
// Substring copying
BString& CopyInto(BString& into, int32 fromOffset,
int32 length) const;
/* returns <into> ref as it's result; doesn't do
* anything if <into> is <this>
*/
void CopyInto(char *into, int32 fromOffset,
void CopyInto(char* into, int32 fromOffset,
int32 length) const;
/* caller guarantees that <into> is large enough */
/*---- Appending -----------------------------------------------------------*/
BString &operator+=(const BString &);
BString &operator+=(const char *);
BString &operator+=(char);
BString &Append(const BString &);
BString &Append(const char *);
// Appending
BString& operator+=(const BString& string);
BString& operator+=(const char* string);
BString& operator+=(char c);
BString &Append(const BString &, int32 length);
BString &Append(const char *, int32 length);
BString &Append(char, int32 count);
BString& Append(const BString& string);
BString& Append(const char* string);
/*---- Prepending ----------------------------------------------------------*/
BString &Prepend(const char *);
BString &Prepend(const BString &);
BString &Prepend(const char *, int32);
BString &Prepend(const BString &, int32);
BString &Prepend(char, int32 count);
BString& Append(const BString& string, int32 length);
BString& Append(const char* string, int32 length);
BString& Append(char c, int32 count);
/*---- Inserting ----------------------------------------------------------*/
BString &Insert(const char *, int32 pos);
BString &Insert(const char *, int32 length, int32 pos);
BString &Insert(const char *, int32 fromOffset,
// Prepending
BString& Prepend(const char* string);
BString& Prepend(const BString& string);
BString& Prepend(const char* string, int32 length);
BString& Prepend(const BString& string, int32 length);
BString& Prepend(char c, int32 count);
// Inserting
BString& Insert(const char* string, int32 pos);
BString& Insert(const char* string, int32 length, int32 pos);
BString& Insert(const char* string, int32 fromOffset,
int32 length, int32 pos);
BString &Insert(const BString &, int32 pos);
BString &Insert(const BString &, int32 length, int32 pos);
BString &Insert(const BString &, int32 fromOffset,
BString& Insert(const BString& string, int32 pos);
BString& Insert(const BString& string, int32 length, int32 pos);
BString& Insert(const BString& string, int32 fromOffset,
int32 length, int32 pos);
BString &Insert(char, int32 count, int32 pos);
BString& Insert(char, int32 count, int32 pos);
/*---- Removing -----------------------------------------------------------*/
BString &Truncate(int32 newLength, bool lazy = true);
/* pass false in <lazy> to ensure freeing up the
* truncated memory
*/
BString &Remove(int32 from, int32 length);
// Removing
BString& Truncate(int32 newLength, bool lazy = true);
BString& Remove(int32 from, int32 length);
BString &RemoveFirst(const BString &);
BString &RemoveLast(const BString &);
BString &RemoveAll(const BString &);
BString& RemoveFirst(const BString& string);
BString& RemoveLast(const BString& string);
BString& RemoveAll(const BString& string);
BString &RemoveFirst(const char *);
BString &RemoveLast(const char *);
BString &RemoveAll(const char *);
BString &RemoveSet(const char *setOfCharsToRemove);
BString &MoveInto(BString &into, int32 from, int32 length);
void MoveInto(char *into, int32 from, int32 length);
/* caller guarantees that <into> is large enough */
BString& RemoveFirst(const char* string);
BString& RemoveLast(const char* string);
BString& RemoveAll(const char* string);
BString& RemoveSet(const char* setOfCharsToRemove);
/*---- Compare functions ---------------------------------------------------*/
bool operator<(const BString &) const;
bool operator<=(const BString &) const;
bool operator==(const BString &) const;
bool operator>=(const BString &) const;
bool operator>(const BString &) const;
bool operator!=(const BString &) const;
bool operator<(const char *) const;
bool operator<=(const char *) const;
bool operator==(const char *) const;
bool operator>=(const char *) const;
bool operator>(const char *) const;
bool operator!=(const char *) const;
/*---- strcmp-style compare functions --------------------------------------*/
int Compare(const BString &) const;
int Compare(const char *) const;
int Compare(const BString &, int32 n) const;
int Compare(const char *, int32 n) const;
int ICompare(const BString &) const;
int ICompare(const char *) const;
int ICompare(const BString &, int32 n) const;
int ICompare(const char *, int32 n) const;
/*---- Searching -----------------------------------------------------------*/
int32 FindFirst(const BString &) const;
int32 FindFirst(const char *) const;
int32 FindFirst(const BString &, int32 fromOffset) const;
int32 FindFirst(const char *, int32 fromOffset) const;
int32 FindFirst(char) const;
int32 FindFirst(char, int32 fromOffset) const;
BString& MoveInto(BString& into, int32 from, int32 length);
void MoveInto(char* into, int32 from, int32 length);
int32 FindLast(const BString &) const;
int32 FindLast(const char *) const;
int32 FindLast(const BString &, int32 beforeOffset) const;
int32 FindLast(const char *, int32 beforeOffset) const;
int32 FindLast(char) const;
int32 FindLast(char, int32 beforeOffset) const;
// Compare functions
bool operator<(const BString& string) const;
bool operator<=(const BString& string) const;
bool operator==(const BString& string) const;
bool operator>=(const BString& string) const;
bool operator>(const BString& string) const;
bool operator!=(const BString& string) const;
int32 IFindFirst(const BString &) const;
int32 IFindFirst(const char *) const;
int32 IFindFirst(const BString &, int32 fromOffset) const;
int32 IFindFirst(const char *, int32 fromOffset) const;
bool operator<(const char* string) const;
bool operator<=(const char* string) const;
bool operator==(const char* string) const;
bool operator>=(const char* string) const;
bool operator>(const char* string) const;
bool operator!=(const char* string) const;
int32 IFindLast(const BString &) const;
int32 IFindLast(const char *) const;
int32 IFindLast(const BString &, int32 beforeOffset) const;
int32 IFindLast(const char *, int32 beforeOffset) const;
// strcmp()-style compare functions
int Compare(const BString& string) const;
int Compare(const char* string) const;
int Compare(const BString& string, int32 length) const;
int Compare(const char* string, int32 length) const;
int ICompare(const BString& string) const;
int ICompare(const char* string) const;
int ICompare(const BString& string, int32 length) const;
int ICompare(const char* string, int32 length) const;
/*---- Replacing -----------------------------------------------------------*/
// Searching
int32 FindFirst(const BString& string) const;
int32 FindFirst(const char* string) const;
int32 FindFirst(const BString& string, int32 fromOffset) const;
int32 FindFirst(const char* string, int32 fromOffset) const;
int32 FindFirst(char c) const;
int32 FindFirst(char c, int32 fromOffset) const;
BString &ReplaceFirst(char replaceThis, char withThis);
BString &ReplaceLast(char replaceThis, char withThis);
BString &ReplaceAll(char replaceThis, char withThis,
int32 FindLast(const BString& string) const;
int32 FindLast(const char* string) const;
int32 FindLast(const BString& string, int32 beforeOffset) const;
int32 FindLast(const char* string, int32 beforeOffset) const;
int32 FindLast(char c) const;
int32 FindLast(char c, int32 beforeOffset) const;
int32 IFindFirst(const BString& string) const;
int32 IFindFirst(const char* string) const;
int32 IFindFirst(const BString& string, int32 fromOffset) const;
int32 IFindFirst(const char* string, int32 fromOffset) const;
int32 IFindLast(const BString& string) const;
int32 IFindLast(const char* string) const;
int32 IFindLast(const BString& string, int32 beforeOffset) const;
int32 IFindLast(const char* string, int32 beforeOffset) const;
// Replacing
BString& ReplaceFirst(char replaceThis, char withThis);
BString& ReplaceLast(char replaceThis, char withThis);
BString& ReplaceAll(char replaceThis, char withThis,
int32 fromOffset = 0);
BString &Replace(char replaceThis, char withThis,
BString& Replace(char replaceThis, char withThis,
int32 maxReplaceCount, int32 fromOffset = 0);
BString &ReplaceFirst(const char *replaceThis,
const char *withThis);
BString &ReplaceLast(const char *replaceThis,
const char *withThis);
BString &ReplaceAll(const char *replaceThis,
const char *withThis, int32 fromOffset = 0);
BString &Replace(const char *replaceThis, const char *withThis,
BString& ReplaceFirst(const char* replaceThis,
const char* withThis);
BString& ReplaceLast(const char* replaceThis,
const char* withThis);
BString& ReplaceAll(const char* replaceThis,
const char* withThis, int32 fromOffset = 0);
BString& Replace(const char* replaceThis, const char* withThis,
int32 maxReplaceCount, int32 fromOffset = 0);
BString &IReplaceFirst(char replaceThis, char withThis);
BString &IReplaceLast(char replaceThis, char withThis);
BString &IReplaceAll(char replaceThis, char withThis,
BString& IReplaceFirst(char replaceThis, char withThis);
BString& IReplaceLast(char replaceThis, char withThis);
BString& IReplaceAll(char replaceThis, char withThis,
int32 fromOffset = 0);
BString &IReplace(char replaceThis, char withThis,
BString& IReplace(char replaceThis, char withThis,
int32 maxReplaceCount, int32 fromOffset = 0);
BString &IReplaceFirst(const char *replaceThis,
const char *withThis);
BString &IReplaceLast(const char *replaceThis,
const char *withThis);
BString &IReplaceAll(const char *replaceThis,
const char *withThis, int32 fromOffset = 0);
BString &IReplace(const char *replaceThis, const char *withThis,
BString& IReplaceFirst(const char* replaceThis,
const char* withThis);
BString& IReplaceLast(const char* replaceThis,
const char* withThis);
BString& IReplaceAll(const char* replaceThis,
const char* withThis, int32 fromOffset = 0);
BString& IReplace(const char* replaceThis, const char* withThis,
int32 maxReplaceCount, int32 fromOffset = 0);
BString &ReplaceSet(const char *setOfChars, char with);
BString &ReplaceSet(const char *setOfChars, const char *with);
/*---- Unchecked char access -----------------------------------------------*/
char operator[](int32 index) const;
char &operator[](int32 index);
/*---- Checked char access -------------------------------------------------*/
char ByteAt(int32 index) const;
BString& ReplaceSet(const char* setOfChars, char with);
BString& ReplaceSet(const char* setOfChars, const char *with);
/*---- Fast low-level manipulation -----------------------------------------*/
char *LockBuffer(int32 maxLength);
/* Make room for characters to be added by C-string like manipulation.
* Returns the equivalent of String(), <maxLength> includes space for
* trailing zero while used as C-string, it is illegal to call other
* BString routines that rely on data/length consistency until
* UnlockBuffer sets things up again.
*/
// Unchecked char access
char operator[](int32 index) const;
char& operator[](int32 index);
BString &UnlockBuffer(int32 length = -1);
/* Finish using BString as C-string, adjusting length. If no length
* passed in, strlen of internal data is used to determine it.
* BString is in consistent state after this.
*/
/*---- Upercase<->Lowercase ------------------------------------------------*/
BString &ToLower();
BString &ToUpper();
// Checked char access
char ByteAt(int32 index) const;
BString &Capitalize();
/* Converts first character to upper-case, rest to
* lower-case
*/
// Fast low-level manipulation
char* LockBuffer(int32 maxLength);
BString& UnlockBuffer(int32 length = -1);
BString &CapitalizeEachWord();
/* Converts first character in each
* non-alphabethycal-character-separated
* word to upper-case, rest to lower-case
*/
/*----- Escaping and Deescaping --------------------------------------------*/
BString &CharacterEscape(const char *original,
const char *setOfCharsToEscape, char escapeWith);
/* copies original into <this>, escaping characters
* specified in <setOfCharsToEscape> by prepending
* them with <escapeWith>
*/
BString &CharacterEscape(const char *setOfCharsToEscape,
// Upercase <-> Lowercase
BString& ToLower();
BString& ToUpper();
BString& Capitalize();
BString& CapitalizeEachWord();
// Escaping and De-escaping
BString& CharacterEscape(const char* original,
const char* setOfCharsToEscape, char escapeWith);
BString& CharacterEscape(const char* setOfCharsToEscape,
char escapeWith);
/* escapes characters specified in <setOfCharsToEscape>
* by prepending them with <escapeWith>
*/
BString &CharacterDeescape(const char *original, char escapeChar);
/* copy <original> into the string removing the escaping
* characters <escapeChar>
*/
BString &CharacterDeescape(char escapeChar);
/* remove the escaping characters <escapeChar> from
* the string
*/
BString& CharacterDeescape(const char* original, char escapeChar);
BString& CharacterDeescape(char escapeChar);
/*---- Simple sprintf replacement calls ------------------------------------*/
/*---- Slower than sprintf but type and overflow safe ----------------------*/
BString &operator<<(const char *);
BString &operator<<(const BString &);
BString &operator<<(char);
BString &operator<<(int);
BString &operator<<(unsigned int);
BString &operator<<(uint32);
BString &operator<<(int32);
BString &operator<<(uint64);
BString &operator<<(int64);
BString &operator<<(float);
/* float output hardcodes %.2f style formatting */
/*----- Private or reserved ------------------------------------------------*/
private:
void _Init(const char *, int32);
void _DoAssign(const char *, int32);
void _DoAppend(const char *, int32);
char *_GrowBy(int32);
char *_OpenAtBy(int32, int32);
char *_ShrinkAtBy(int32, int32);
void _DoPrepend(const char *, int32);
int32 _FindAfter(const char *, int32, int32) const;
int32 _IFindAfter(const char *, int32, int32) const;
int32 _ShortFindAfter(const char *, int32) const;
int32 _FindBefore(const char *, int32, int32) const;
int32 _IFindBefore(const char *, int32, int32) const;
BString &_DoReplace(const char *, const char *, int32, int32,
bool);
void _SetLength(int32);
// Slower than sprintf() but type and overflow safe
BString& operator<<(const char* string);
BString& operator<<(const BString& string);
BString& operator<<(char c);
BString& operator<<(int value);
BString& operator<<(unsigned int value);
BString& operator<<(uint32 value);
BString& operator<<(int32 value);
BString& operator<<(uint64 value);
BString& operator<<(int64 value);
BString& operator<<(float value);
// float output hardcodes %.2f style formatting
private:
void _Init(const char *, int32);
void _DoAssign(const char *, int32);
void _DoAppend(const char *, int32);
char* _GrowBy(int32);
char* _OpenAtBy(int32, int32);
char* _ShrinkAtBy(int32, int32);
void _DoPrepend(const char *, int32);
int32 _FindAfter(const char *, int32, int32) const;
int32 _IFindAfter(const char *, int32, int32) const;
int32 _ShortFindAfter(const char *, int32) const;
int32 _FindBefore(const char *, int32, int32) const;
int32 _IFindBefore(const char *, int32, int32) const;
BString& _DoReplace(const char *, const char *, int32, int32,
bool);
void _SetLength(int32);
#if DEBUG
void _SetUsingAsCString(bool);
void _AssertNotUsingAsCString() const;
void _SetUsingAsCString(bool);
void _AssertNotUsingAsCString() const;
#else
void _SetUsingAsCString(bool) {}
void _AssertNotUsingAsCString() const {}
void _SetUsingAsCString(bool) {}
void _AssertNotUsingAsCString() const {}
#endif
char *_Alloc( int32);
char* _Alloc(int32 size);
struct PosVect;
void _ReplaceAtPositions( const PosVect* positions,
int32 searchLen,
const char* with,
int32 withLen);
class PosVect;
void _ReplaceAtPositions(const PosVect* positions,
int32 searchLength, const char* with, int32 withLen);
protected:
char *_privateData;
protected:
char* fPrivateData;
};
/*----- Comutative compare operators --------------------------------------*/
bool operator<(const char *, const BString &);
bool operator<=(const char *, const BString &);
bool operator==(const char *, const BString &);
bool operator>(const char *, const BString &);
bool operator>=(const char *, const BString &);
bool operator!=(const char *, const BString &);
// Commutative compare operators
bool operator<(const char* a, const BString& b);
bool operator<=(const char* a, const BString& b);
bool operator==(const char* a, const BString& b);
bool operator>(const char* a, const BString& b);
bool operator>=(const char* a, const BString& b);
bool operator!=(const char* a, const BString& b);
/*----- Non-member compare for sorting, etc. ------------------------------*/
int Compare(const BString &, const BString &);
int ICompare(const BString &, const BString &);
int Compare(const BString *, const BString *);
int ICompare(const BString *, const BString *);
// Non-member compare for sorting, etc.
int Compare(const BString& a, const BString& b);
int ICompare(const BString& a, const BString& b);
int Compare(const BString* a, const BString* b);
int ICompare(const BString* a, const BString* b);
/*-------------------------------------------------------------------------*/
/*---- No user serviceable parts after this -------------------------------*/
inline int32
BString::Length() const
{
return _privateData ? (*((int32 *)_privateData - 1) & 0x7fffffff) : 0;
return fPrivateData ? (*((int32 *)fPrivateData - 1) & 0x7fffffff) : 0;
/* the most significant bit is reserved; accessing
* it in any way will cause the computer to explode
*/
@@ -363,29 +284,29 @@ BString::Length() const
inline const char *
BString::String() const
{
if (!_privateData)
if (!fPrivateData)
return "";
return _privateData;
return fPrivateData;
}
inline BString &
BString::SetTo(const char *str)
BString::SetTo(const char *string)
{
return operator=(str);
return operator=(string);
}
inline char
BString::operator[](int32 index) const
{
return _privateData[index];
return fPrivateData[index];
}
inline char
BString::ByteAt(int32 index) const
{
if (!_privateData || index < 0 || index > Length())
if (!fPrivateData || index < 0 || index > Length())
return 0;
return _privateData[index];
return fPrivateData[index];
}
inline BString &