Back to index

lightning-sunbird  0.9+nobinonly
txStringUtils.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Axel Hecht <axel@pike.org>
00024  *   Peter Van der Beken <peterv@propagandism.org>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "txStringUtils.h"
00041 #include "nsDependentString.h"
00042 
00043 int
00044 txCaseInsensitiveStringComparator::operator()(const char_type* lhs,
00045                                               const char_type* rhs,
00046                                               PRUint32 aLength ) const
00047 {
00048   PRUnichar thisChar, otherChar;
00049   PRUint32 compLoop = 0;
00050   while (compLoop < aLength) {
00051     thisChar = lhs[compLoop];
00052     if ((thisChar >= 'A') && (thisChar <= 'Z')) {
00053       thisChar += 32;
00054     }
00055     otherChar = rhs[compLoop];
00056     if ((otherChar >= 'A') && (otherChar <= 'Z')) {
00057       otherChar += 32;
00058     }
00059     if (thisChar != otherChar) {
00060       return thisChar - otherChar;
00061     }
00062     ++compLoop;
00063   }
00064   return 0;
00065 
00066 }
00067 
00068 int
00069 txCaseInsensitiveStringComparator::operator()(char_type lhs,
00070                                               char_type rhs) const
00071 {
00072   if (lhs >= 'A' && lhs <= 'Z') {
00073     lhs += 32;
00074   }
00075   if (rhs >= 'A' && rhs <= 'Z') {
00076     rhs += 32;
00077   }
00078   return lhs - rhs;
00079 } 
00080 
00084 class ConvertToLowerCase
00085 {
00086 public:
00087   typedef PRUnichar value_type;
00088 
00089   PRUint32 write( const PRUnichar* aSource, PRUint32 aSourceLength)
00090   {
00091     PRUnichar* cp = NS_CONST_CAST(PRUnichar*, aSource);
00092     const PRUnichar* end = aSource + aSourceLength;
00093     while (cp != end) {
00094       PRUnichar ch = *cp;
00095       if ((ch >= 'A') && (ch <= 'Z'))
00096         *cp = ch + ('a' - 'A');
00097       ++cp;
00098     }
00099     return aSourceLength;
00100   }
00101 };
00102 
00103 void TX_ToLowerCase(nsAString& aString)
00104 {
00105   nsAString::iterator fromBegin, fromEnd;
00106   ConvertToLowerCase converter;
00107   copy_string(aString.BeginWriting(fromBegin), aString.EndWriting(fromEnd),
00108               converter);
00109 }
00110 
00114 class CopyToLowerCase
00115 {
00116 public:
00117   typedef PRUnichar value_type;
00118 
00119   CopyToLowerCase(nsAString::iterator& aDestIter) : mIter(aDestIter)
00120   {
00121   }
00122 
00123   PRUint32 write(const PRUnichar* aSource, PRUint32 aSourceLength)
00124   {
00125     PRUint32 len = PR_MIN(PRUint32(mIter.size_forward()), aSourceLength);
00126     PRUnichar* cp = mIter.get();
00127     const PRUnichar* end = aSource + len;
00128     while (aSource != end) {
00129       PRUnichar ch = *aSource;
00130       if ((ch >= 'A') && (ch <= 'Z'))
00131         *cp = ch + ('a' - 'A');
00132       else
00133         *cp = ch;
00134       ++aSource;
00135       ++cp;
00136     }
00137     mIter.advance(len);
00138     return len;
00139   }
00140 
00141 protected:
00142   nsAString::iterator& mIter;
00143 };
00144 
00145 void TX_ToLowerCase(const nsAString& aSource, nsAString& aDest)
00146 {
00147   nsAString::const_iterator fromBegin, fromEnd;
00148   nsAString::iterator toBegin;
00149   if (!EnsureStringLength(aDest, aSource.Length()))
00150     return; // XXX no way to signal out-of-memory
00151   CopyToLowerCase converter(aDest.BeginWriting(toBegin));
00152   copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd),
00153               converter);
00154 }