nsTSubstring.h revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla.
*
* The Initial Developer of the Original Code is IBM Corporation.
* Portions created by IBM Corporation are Copyright (C) 2003
* IBM Corporation. All Rights Reserved.
*
* Contributor(s):
* Darin Fisher <darin@meer.net>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/**
* nsTSubstring
*
* The base string type. This type is not instantiated directly. A sub-
* class is instantiated instead. For example, see nsTString.
*
* This type works like nsTAString except that it does not have the ABI
* requirements of that interface. Like nsTAString, nsTSubstring
* represents a single contiguous array of characters that may or may not
* be null-terminated.
*
* Many of the accessors on nsTSubstring are inlined as an optimization.
*
* This class is also known as "nsASingleFragmentC?String".
*/
{
typedef nsTSubstring_CharT self_type;
typedef nsTString_CharT string_type;
typedef char_type* char_iterator;
typedef const char_type* const_char_iterator;
/**
* reading iterators
*/
/**
* deprecated reading iterators
*/
{
return iter;
}
{
return iter;
}
{
}
{
}
/**
* writing iterators
*/
/**
* deprecated writing iterators
*/
{
return iter;
}
{
return iter;
}
{
}
{
}
/**
* accessors
*/
// returns pointer to string data (not necessarily null-terminated)
{
return mData;
}
{
return mLength;
}
{
return mLength == 0;
}
{
}
PRBool IsTerminated() const
{
return mFlags & F_TERMINATED;
}
{
return mData[i];
}
{
return CharAt(i);
}
{
return mData[0];
}
inline
{
}
/**
* equality
*/
NS_COM PRBool NS_FASTCALL Equals( const abstract_string_type& readable, const comparator_type& comp ) const;
/**
* An efficient comparison with ASCII that can be used even
* for wide strings. Call this version when you know the
* length of 'data'.
*/
/**
* An efficient comparison with ASCII that can be used even
* for wide strings. Call this version when 'data' is
* null-terminated.
*/
// EqualsLiteral must ONLY be applied to an actual literal string.
// Do not attempt to use it with a regular char* pointer, or with a char
// array variable.
// The template trick to acquire the array length at compile time without
// using a macro is due to Corey Kosak, with much thanks.
#ifdef NS_DISABLE_LITERAL_TEMPLATE
{
return EqualsASCII(str);
}
#else
template<int N>
{
}
template<int N>
{
const char* s = str;
return EqualsASCII(s, N-1);
}
#endif
// The LowerCaseEquals methods compare the lower case version of
// *not* lowercased for you. If you compare to an ASCII or literal
// string that contains an uppercase character, it is guaranteed to
// return false. We will throw assertions too.
// LowerCaseEqualsLiteral must ONLY be applied to an actual
// literal string. Do not attempt to use it with a regular char*
// pointer, or with a char array variable. Use
// LowerCaseEqualsASCII for them.
#ifdef NS_DISABLE_LITERAL_TEMPLATE
{
return LowerCaseEqualsASCII(str);
}
#else
template<int N>
{
}
template<int N>
{
const char* s = str;
return LowerCaseEqualsASCII(s, N-1);
}
#endif
/**
* assignment
*/
// AssignLiteral must ONLY be applied to an actual literal string.
// Do not attempt to use it with a regular char* pointer, or with a char
// array variable. Use AssignASCII for those.
#ifdef NS_DISABLE_LITERAL_TEMPLATE
void AssignLiteral( const char* str )
{ AssignASCII(str); }
#else
template<int N>
void AssignLiteral( const char (&str)[N] )
template<int N>
void AssignLiteral( char (&str)[N] )
#endif
/**
* buffer manipulation
*/
void Replace( index_type cutStart, size_type cutLength, char_type c ) { Replace(cutStart, cutLength, &c, 1); }
NS_COM void NS_FASTCALL Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) );
void Replace( index_type cutStart, size_type cutLength, const self_type& str ) { Replace(cutStart, cutLength, str.Data(), str.Length()); }
NS_COM void NS_FASTCALL Replace( index_type cutStart, size_type cutLength, const substring_tuple_type& tuple );
NS_COM void NS_FASTCALL Replace( index_type cutStart, size_type cutLength, const abstract_string_type& readable );
NS_COM void NS_FASTCALL ReplaceASCII( index_type cutStart, size_type cutLength, const char* data, size_type length = size_type(-1) );
void Append( const char_type* data, size_type length = size_type(-1) ) { Replace(mLength, 0, data, length); }
void AppendASCII( const char* data, size_type length = size_type(-1) ) { ReplaceASCII(mLength, 0, data, length); }
// AppendLiteral must ONLY be applied to an actual literal string.
// Do not attempt to use it with a regular char* pointer, or with a char
// array variable. Use AppendASCII for those.
#ifdef NS_DISABLE_LITERAL_TEMPLATE
void AppendLiteral( const char* str )
{ AppendASCII(str); }
#else
template<int N>
void AppendLiteral( const char (&str)[N] )
template<int N>
void AppendLiteral( char (&str)[N] )
#endif
void Insert( const char_type* data, index_type pos, size_type length = size_type(-1) ) { Replace(pos, 0, data, length); }
void Cut( index_type cutStart, size_type cutLength ) { Replace(cutStart, cutLength, char_traits::sEmptyBuffer, 0); }
/**
* buffer sizing
*/
{
}
/**
* string data is never null, but can be marked void. if true, the
* string will be truncated. @see nsTSubstring::IsVoid
*/
/**
* this is public to support automatic conversion of tuple to string
* base type, which helps avoid converting to nsTAString.
*/
{
}
// XXX GCC 3.4 needs this :-(
// default initialization
// allow subclasses to initialize fields directly
// version of constructor that leaves mData and mLength uninitialized
: abstract_string_type(flags) {}
// copy-constructor, constructs as dependent on given object
// (NOTE: this is for internal use only)
/**
* this function releases mData and does not change the value of
* any of its member variables. inotherwords, this function acts
* like a destructor.
*/
void NS_FASTCALL Finalize();
/**
* this function prepares mData to be mutated.
*
* @param capacity specifies the required capacity of mData
* @param old_data returns null or the old value of mData
* @param old_flags returns 0 or the old value of mFlags
*
* if mData is already mutable and of sufficient capacity, then this
* function will return immediately. otherwise, it will either resize
* mData or allocate a new shared buffer. if it needs to allocate a
* new buffer, then it will return the old buffer and the corresponding
* flags. this allows the caller to decide when to free the old data.
*
* XXX we should expose a way for subclasses to free old_data.
*/
/**
* this function prepares a section of mData to be modified. if
* necessary, this function will reallocate mData and possibly move
* existing data to open up the specified section.
*
* @param cutStart specifies the starting offset of the section
* @param cutLength specifies the length of the section to be replaced
* @param newLength specifies the length of the new section
*
* for example, suppose mData contains the string "abcdef" then
*
* ReplacePrep(2, 3, 4);
*
* would cause mData to look like "ab____f" where the characters
* indicated by '_' have an unspecified value and can be freely
* modified. this function will null-terminate mData upon return.
*/
/**
* returns the number of writable storage units starting at mData.
* the value does not include space for the null-terminator character.
*
* NOTE: this function returns size_type(-1) if mData is immutable.
*/
/**
* this helper function can be called prior to directly manipulating
* the contents of mData. see, for example, BeginWriting.
*/
/**
* returns true if this string overlaps with the given string fragment.
*/
{
/**
* if it _isn't_ the case that one fragment starts after the other ends,
* or ends before the other starts, then, they conflict:
*
* !(f2.begin >= f1.end || f2.end <= f1.begin)
*
* Simplified, that gives us:
*/
}
/**
* this helper function stores the specified dataFlags in mFlags
*/
{
}
// mFlags is a bitwise combination of the following flags. the meaning
// and interpretation of these flags is an implementation detail.
//
// NOTE: these flags are declared public _only_ for convenience inside
// the string implementation.
enum
{
F_NONE = 0, // no flags
// data flags are in the lower 16-bits
// class flags are in the upper 16-bits
};
//
// Some terminology:
//
// "dependent buffer" A dependent buffer is one that the string class
// does not own. The string class relies on some
// external code to ensure the lifetime of the
// dependent buffer.
//
// "shared buffer" A shared buffer is one that the string class
// allocates. When it allocates a shared string
// buffer, it allocates some additional space at
// the beginning of the buffer for additional
// fields, including a reference count and a
// buffer length. See nsStringHeader.
//
// "adopted buffer" An adopted buffer is a raw string buffer
// allocated on the heap (using nsMemory::Alloc)
// of which the string class subsumes ownership.
//
// Some comments about the string flags:
//
// F_SHARED, F_OWNED, and F_FIXED are all mutually exlusive. They
// indicate the allocation type of mData. If none of these flags
// are set, then the string buffer is dependent.
//
// F_SHARED, F_OWNED, or F_FIXED imply F_TERMINATED. This is because
// the string classes always allocate null-terminated buffers, and
// non-terminated substrings are always dependent.
//
// F_VOIDED implies F_TERMINATED, and moreover it implies that mData
// points to char_traits::sEmptyBuffer. Therefore, F_VOIDED is
// mutually exclusive with F_SHARED, F_OWNED, and F_FIXED.
//
};