/* -*- 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 * http://www.mozilla.org/MPL/ * * 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 * * 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 ***** */ /** * nsTString::Find * * aOffset specifies starting index * aCount specifies number of string compares (iterations) */ PRInt32 nsTString_CharT::Find( const nsCString& aString, bool aIgnoreCase, PRInt32 aOffset, PRInt32 aCount) const { // this method changes the meaning of aOffset and aCount: Find_ComputeSearchRange(mLength, aString.Length(), aOffset, aCount); PRInt32 result = FindSubstring(mData + aOffset, aCount, aString.get(), aString.Length(), aIgnoreCase); if (result != kNotFound) result += aOffset; return result; } PRInt32 nsTString_CharT::Find( const char* aString, bool aIgnoreCase, PRInt32 aOffset, PRInt32 aCount) const { return Find(nsDependentCString(aString), aIgnoreCase, aOffset, aCount); } /** * nsTString::RFind * * aOffset specifies starting index * aCount specifies number of string compares (iterations) */ PRInt32 nsTString_CharT::RFind( const nsCString& aString, bool aIgnoreCase, PRInt32 aOffset, PRInt32 aCount) const { // this method changes the meaning of aOffset and aCount: RFind_ComputeSearchRange(mLength, aString.Length(), aOffset, aCount); PRInt32 result = RFindSubstring(mData + aOffset, aCount, aString.get(), aString.Length(), aIgnoreCase); if (result != kNotFound) result += aOffset; return result; } PRInt32 nsTString_CharT::RFind( const char* aString, bool aIgnoreCase, PRInt32 aOffset, PRInt32 aCount) const { return RFind(nsDependentCString(aString), aIgnoreCase, aOffset, aCount); } /** * nsTString::RFindChar */ PRInt32 nsTString_CharT::RFindChar( PRUnichar aChar, PRInt32 aOffset, PRInt32 aCount) const { return nsBufferRoutines::rfind_char(mData, mLength, aOffset, aChar, aCount); } /** * nsTString::FindCharInSet */ PRInt32 nsTString_CharT::FindCharInSet( const char* aSet, PRInt32 aOffset ) const { if (aOffset < 0) aOffset = 0; else if (aOffset >= PRInt32(mLength)) return kNotFound; PRInt32 result = ::FindCharInSet(mData + aOffset, mLength - aOffset, aSet); if (result != kNotFound) result += aOffset; return result; } /** * nsTString::RFindCharInSet */ PRInt32 nsTString_CharT::RFindCharInSet( const CharT* aSet, PRInt32 aOffset ) const { // We want to pass a "data length" to ::RFindCharInSet if (aOffset < 0 || aOffset > PRInt32(mLength)) aOffset = mLength; else ++aOffset; return ::RFindCharInSet(mData, aOffset, aSet); } // it's a shame to replicate this code. it was done this way in the past // to help performance. this function also gets to keep the rickg style // indentation :-/ PRInt32 nsTString_CharT::ToInteger( PRInt32* aErrorCode, PRUint32 aRadix ) const { CharT* cp=mData; PRInt32 theRadix=10; // base 10 unless base 16 detected, or overriden (aRadix != kAutoDetect) PRInt32 result=0; bool negate=false; CharT theChar=0; //initial value, override if we find an integer *aErrorCode=NS_ERROR_ILLEGAL_VALUE; if(cp) { //begin by skipping over leading chars that shouldn't be part of the number... CharT* endcp=cp+mLength; bool done=false; while((cp='A') && (theChar<='F')) { if(10==theRadix) { if(kAutoDetect==aRadix){ theRadix=16; cp=first; //backup result=0; haveValue = false; } else { *aErrorCode=NS_ERROR_ILLEGAL_VALUE; result=0; break; } } else { result = (theRadix * result) + ((theChar-'A')+10); haveValue = true; } } else if((theChar>='a') && (theChar<='f')) { if(10==theRadix) { if(kAutoDetect==aRadix){ theRadix=16; cp=first; //backup result=0; haveValue = false; } else { *aErrorCode=NS_ERROR_ILLEGAL_VALUE; result=0; break; } } else { result = (theRadix * result) + ((theChar-'a')+10); haveValue = true; } } else if((('X'==theChar) || ('x'==theChar)) && (!haveValue || result == 0)) { continue; } else if((('#'==theChar) || ('+'==theChar)) && !haveValue) { continue; } else { //we've encountered a char that's not a legal number or sign break; } if (result < oldresult) { // overflow! *aErrorCode = NS_ERROR_ILLEGAL_VALUE; result = 0; break; } } //while if(negate) result=-result; } //if } return result; } /** * nsTString::Mid */ PRUint32 nsTString_CharT::Mid( self_type& aResult, index_type aStartPos, size_type aLengthToCopy ) const { if (aStartPos == 0 && aLengthToCopy >= mLength) aResult = *this; else aResult = Substring(*this, aStartPos, aLengthToCopy); return aResult.mLength; } /** * nsTString::SetCharAt */ bool nsTString_CharT::SetCharAt( PRUnichar aChar, PRUint32 aIndex ) { if (aIndex >= mLength) return false; if (!EnsureMutable()) NS_RUNTIMEABORT("OOM"); mData[aIndex] = CharT(aChar); return true; } /** * nsTString::StripChars,StripChar,StripWhitespace */ void nsTString_CharT::StripChars( const char* aSet ) { if (!EnsureMutable()) NS_RUNTIMEABORT("OOM"); mLength = nsBufferRoutines::strip_chars(mData, mLength, aSet); } void nsTString_CharT::StripWhitespace() { StripChars(kWhitespace); } /** * nsTString::ReplaceChar,ReplaceSubstring */ void nsTString_CharT::ReplaceChar( char_type aOldChar, char_type aNewChar ) { if (!EnsureMutable()) // XXX do this lazily? NS_RUNTIMEABORT("OOM"); for (PRUint32 i=0; i 2 && mData[0] == mData[mLength - 1] && (mData[0] == '\'' || mData[0] == '"')) { ++start; --end; } PRUint32 setLen = nsCharTraits::length(aSet); if (aTrimLeading) { PRUint32 cutStart = start - mData; PRUint32 cutLength = 0; // walk forward from start to end for (; start != end; ++start, ++cutLength) { PRInt32 pos = FindChar1(aSet, setLen, 0, *start, setLen); if (pos == kNotFound) break; } if (cutLength) { Cut(cutStart, cutLength); // reset iterators start = mData + cutStart; end = mData + mLength - cutStart; } } if (aTrimTrailing) { PRUint32 cutEnd = end - mData; PRUint32 cutLength = 0; // walk backward from end to start --end; for (; end >= start; --end, ++cutLength) { PRInt32 pos = FindChar1(aSet, setLen, 0, *end, setLen); if (pos == kNotFound) break; } if (cutLength) Cut(cutEnd - cutLength, cutLength); } } /** * nsTString::CompressWhitespace */ void nsTString_CharT::CompressWhitespace( bool aTrimLeading, bool aTrimTrailing ) { const char* set = kWhitespace; ReplaceChar(set, ' '); Trim(set, aTrimLeading, aTrimTrailing); // this one does some questionable fu... just copying the old code! mLength = nsBufferRoutines::compress_chars(mData, mLength, set); } /** * nsTString::AssignWithConversion */ void nsTString_CharT::AssignWithConversion( const incompatible_char_type* aData, PRInt32 aLength ) { // for compatibility with the old string implementation, we need to allow // for a NULL input buffer :-( if (!aData) { Truncate(); } else { if (aLength < 0) aLength = nsCharTraits::length(aData); AssignWithConversion(Substring(aData, aLength)); } } /** * nsTString::AppendWithConversion */ void nsTString_CharT::AppendWithConversion( const incompatible_char_type* aData, PRInt32 aLength ) { // for compatibility with the old string implementation, we need to allow // for a NULL input buffer :-( if (aData) { if (aLength < 0) aLength = nsCharTraits::length(aData); AppendWithConversion(Substring(aData, aLength)); } }