2002-05-09 00:48:26 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2001-09-27 00:53:57 +04:00
|
|
|
*
|
2002-05-09 00:48:26 +04:00
|
|
|
* 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/
|
2001-09-27 00:53:57 +04:00
|
|
|
*
|
2002-05-09 00:48:26 +04:00
|
|
|
* 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.
|
2001-09-27 00:53:57 +04:00
|
|
|
*
|
2002-05-09 00:48:26 +04:00
|
|
|
* The Original Code is Unicode case conversion helpers.
|
2001-09-27 00:53:57 +04:00
|
|
|
*
|
2002-05-09 00:48:26 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corp..
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2002
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2001-09-27 00:53:57 +04:00
|
|
|
*
|
|
|
|
* Contributor(s):
|
2002-05-09 00:48:26 +04:00
|
|
|
* Alec Flett <alecf@netscape.com>
|
2006-06-07 20:25:40 +04:00
|
|
|
* Benjamin Smedberg <benjamin@smedbergs.us>
|
2006-09-27 21:44:27 +04:00
|
|
|
* Ben Turner <mozilla@songbirdnest.com>
|
2002-05-09 00:48:26 +04:00
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
2001-09-27 00:53:57 +04:00
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsUnicharUtilCIID.h"
|
2006-09-27 21:44:27 +04:00
|
|
|
|
2006-12-01 22:26:18 +03:00
|
|
|
#include "nsCRT.h"
|
2006-09-27 21:44:27 +04:00
|
|
|
#include "nsICaseConversion.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsXPCOMStrings.h"
|
2001-10-20 02:51:11 +04:00
|
|
|
|
2006-06-07 20:25:40 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2006-12-01 22:26:18 +03:00
|
|
|
static nsICaseConversion* gCaseConv = nsnull;
|
2001-09-27 00:53:57 +04:00
|
|
|
|
2006-12-01 22:26:18 +03:00
|
|
|
nsICaseConversion*
|
2006-09-27 21:44:27 +04:00
|
|
|
NS_GetCaseConversion()
|
|
|
|
{
|
2006-12-01 22:26:18 +03:00
|
|
|
if (!gCaseConv) {
|
|
|
|
nsresult rv = CallGetService(NS_UNICHARUTIL_CONTRACTID, &gCaseConv);
|
2006-06-21 19:25:17 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2006-12-01 22:26:18 +03:00
|
|
|
NS_ERROR("Failed to get the case conversion service!");
|
|
|
|
gCaseConv = nsnull;
|
2001-10-20 02:51:11 +04:00
|
|
|
}
|
2006-09-27 21:44:27 +04:00
|
|
|
}
|
|
|
|
return gCaseConv;
|
|
|
|
}
|
2001-12-23 05:56:41 +03:00
|
|
|
|
|
|
|
void
|
2006-09-27 21:44:27 +04:00
|
|
|
ToLowerCase(nsAString& aString)
|
|
|
|
{
|
2006-12-01 22:26:18 +03:00
|
|
|
nsICaseConversion* caseConv = NS_GetCaseConversion();
|
2006-09-27 21:44:27 +04:00
|
|
|
if (caseConv) {
|
|
|
|
PRUnichar *buf = aString.BeginWriting();
|
|
|
|
caseConv->ToLower(buf, buf, aString.Length());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
NS_WARNING("No case converter: no conversion done");
|
|
|
|
}
|
2001-12-08 10:08:38 +03:00
|
|
|
|
2006-09-27 21:44:27 +04:00
|
|
|
void
|
|
|
|
ToLowerCase(const nsAString& aSource,
|
|
|
|
nsAString& aDest)
|
|
|
|
{
|
|
|
|
const PRUnichar *in;
|
|
|
|
PRUint32 len = NS_StringGetData(aSource, &in);
|
2001-12-08 10:08:38 +03:00
|
|
|
|
2006-09-27 21:44:27 +04:00
|
|
|
PRUnichar *out;
|
|
|
|
NS_StringGetMutableData(aDest, len, &out);
|
2001-12-08 10:08:38 +03:00
|
|
|
|
2006-12-01 22:26:18 +03:00
|
|
|
nsICaseConversion* caseConv = NS_GetCaseConversion();
|
2006-09-27 21:44:27 +04:00
|
|
|
if (out && caseConv)
|
|
|
|
caseConv->ToLower(in, out, len);
|
|
|
|
else {
|
|
|
|
NS_WARNING("No case converter: only copying");
|
|
|
|
aDest.Assign(aSource);
|
2001-09-27 00:53:57 +04:00
|
|
|
}
|
2006-09-27 21:44:27 +04:00
|
|
|
}
|
2001-09-27 00:53:57 +04:00
|
|
|
|
2001-12-23 05:56:41 +03:00
|
|
|
void
|
2006-09-27 21:44:27 +04:00
|
|
|
ToUpperCase(nsAString& aString)
|
|
|
|
{
|
2006-12-01 22:26:18 +03:00
|
|
|
nsICaseConversion* caseConv = NS_GetCaseConversion();
|
2006-09-27 21:44:27 +04:00
|
|
|
if (caseConv) {
|
|
|
|
PRUnichar *buf = aString.BeginWriting();
|
|
|
|
caseConv->ToUpper(buf, buf, aString.Length());
|
2001-12-23 05:56:41 +03:00
|
|
|
}
|
2006-09-27 21:44:27 +04:00
|
|
|
else
|
|
|
|
NS_WARNING("No case converter: no conversion done");
|
|
|
|
}
|
2001-12-23 05:56:41 +03:00
|
|
|
|
|
|
|
void
|
2006-09-27 21:44:27 +04:00
|
|
|
ToUpperCase(const nsAString& aSource,
|
|
|
|
nsAString& aDest)
|
|
|
|
{
|
|
|
|
const PRUnichar *in;
|
|
|
|
PRUint32 len = NS_StringGetData(aSource, &in);
|
2001-12-08 10:08:38 +03:00
|
|
|
|
2006-09-27 21:44:27 +04:00
|
|
|
PRUnichar *out;
|
|
|
|
NS_StringGetMutableData(aDest, len, &out);
|
2001-12-08 10:08:38 +03:00
|
|
|
|
2006-12-01 22:26:18 +03:00
|
|
|
nsICaseConversion* caseConv = NS_GetCaseConversion();
|
2006-09-27 21:44:27 +04:00
|
|
|
if (out && caseConv)
|
|
|
|
caseConv->ToUpper(in, out, len);
|
|
|
|
else {
|
|
|
|
NS_WARNING("No case converter: only copying");
|
|
|
|
aDest.Assign(aSource);
|
2001-12-08 10:08:38 +03:00
|
|
|
}
|
2006-09-27 21:44:27 +04:00
|
|
|
}
|
2001-12-08 10:08:38 +03:00
|
|
|
|
2006-06-07 20:25:40 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2001-09-27 00:53:57 +04:00
|
|
|
|
2006-09-27 21:44:27 +04:00
|
|
|
PRInt32
|
|
|
|
nsCaseInsensitiveStringComparator::operator()(const PRUnichar* lhs,
|
|
|
|
const PRUnichar* rhs,
|
|
|
|
PRUint32 aLength) const
|
|
|
|
{
|
|
|
|
PRInt32 result;
|
2006-12-01 22:26:18 +03:00
|
|
|
nsICaseConversion* caseConv = NS_GetCaseConversion();
|
2006-09-27 21:44:27 +04:00
|
|
|
if (caseConv)
|
|
|
|
caseConv->CaseInsensitiveCompare(lhs, rhs, aLength, &result);
|
|
|
|
else {
|
|
|
|
NS_WARNING("No case converter: using default");
|
|
|
|
nsDefaultStringComparator comparator;
|
|
|
|
result = comparator(lhs, rhs, aLength);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2001-10-10 03:41:57 +04:00
|
|
|
|
2006-09-27 21:44:27 +04:00
|
|
|
PRInt32
|
|
|
|
nsCaseInsensitiveStringComparator::operator()(PRUnichar lhs,
|
|
|
|
PRUnichar rhs) const
|
|
|
|
{
|
|
|
|
// see if they're an exact match first
|
|
|
|
if (lhs == rhs)
|
|
|
|
return 0;
|
|
|
|
|
2006-12-01 22:26:18 +03:00
|
|
|
nsICaseConversion* caseConv = NS_GetCaseConversion();
|
2006-09-27 21:44:27 +04:00
|
|
|
if (caseConv) {
|
|
|
|
caseConv->ToLower(lhs, &lhs);
|
|
|
|
caseConv->ToLower(rhs, &rhs);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (lhs < 256)
|
|
|
|
lhs = tolower(char(lhs));
|
|
|
|
if (rhs < 256)
|
|
|
|
rhs = tolower(char(rhs));
|
|
|
|
NS_WARNING("No case converter: no conversion done");
|
2001-10-10 03:41:57 +04:00
|
|
|
}
|
2006-09-27 21:44:27 +04:00
|
|
|
|
|
|
|
if (lhs == rhs)
|
|
|
|
return 0;
|
|
|
|
else if (lhs < rhs)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // MOZILLA_INTERNAL_API
|
|
|
|
|
2006-06-07 20:25:40 +04:00
|
|
|
PRInt32
|
2006-09-27 21:44:27 +04:00
|
|
|
CaseInsensitiveCompare(const PRUnichar *a,
|
|
|
|
const PRUnichar *b,
|
|
|
|
PRUint32 len)
|
2006-06-07 20:25:40 +04:00
|
|
|
{
|
2006-12-01 22:26:18 +03:00
|
|
|
nsICaseConversion* caseConv = NS_GetCaseConversion();
|
2006-09-27 21:44:27 +04:00
|
|
|
if (!caseConv)
|
|
|
|
return NS_strcmp(a, b);
|
2006-06-07 20:25:40 +04:00
|
|
|
|
2006-09-27 21:44:27 +04:00
|
|
|
PRInt32 result;
|
|
|
|
caseConv->CaseInsensitiveCompare(a, b, len, &result);
|
|
|
|
return result;
|
2006-06-07 20:25:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MOZILLA_INTERNAL_API
|
2001-10-10 03:41:57 +04:00
|
|
|
|
2001-10-20 02:51:11 +04:00
|
|
|
PRUnichar
|
|
|
|
ToLowerCase(PRUnichar aChar)
|
|
|
|
{
|
2006-09-27 21:44:27 +04:00
|
|
|
PRUnichar result;
|
2006-12-01 22:26:18 +03:00
|
|
|
nsICaseConversion* caseConv = NS_GetCaseConversion();
|
2006-09-27 21:44:27 +04:00
|
|
|
if (caseConv)
|
|
|
|
caseConv->ToLower(aChar, &result);
|
|
|
|
else {
|
|
|
|
NS_WARNING("No case converter: no conversion done");
|
|
|
|
if (aChar < 256)
|
|
|
|
result = tolower(char(aChar));
|
|
|
|
else
|
|
|
|
result = aChar;
|
|
|
|
}
|
|
|
|
return result;
|
2001-10-20 02:51:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUnichar
|
|
|
|
ToUpperCase(PRUnichar aChar)
|
|
|
|
{
|
2006-09-27 21:44:27 +04:00
|
|
|
PRUnichar result;
|
2006-12-01 22:26:18 +03:00
|
|
|
nsICaseConversion* caseConv = NS_GetCaseConversion();
|
2006-09-27 21:44:27 +04:00
|
|
|
if (caseConv)
|
|
|
|
caseConv->ToUpper(aChar, &result);
|
|
|
|
else {
|
|
|
|
NS_WARNING("No case converter: no conversion done");
|
|
|
|
if (aChar < 256)
|
|
|
|
result = toupper(char(aChar));
|
|
|
|
else
|
|
|
|
result = aChar;
|
|
|
|
}
|
|
|
|
return result;
|
2001-10-20 02:51:11 +04:00
|
|
|
}
|