From 801a4fe99f63e01164e2e24bfff5f55c30626f79 Mon Sep 17 00:00:00 2001 From: "ftang%netscape.com" Date: Mon, 8 Feb 1999 18:57:21 +0000 Subject: [PATCH] change upper and lower case operation to call unicharutil for the nonASCII case git-svn-id: svn://10.0.0.236/trunk@20011 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/base/src/makefile.win | 2 +- mozilla/base/src/nsCRT.cpp | 72 +++++++++++++++-- mozilla/base/src/nsString.cpp | 93 ++++++++++++++++++++++ mozilla/string/obsolete/nsString.cpp | 93 ++++++++++++++++++++++ mozilla/xpcom/ds/nsCRT.cpp | 72 +++++++++++++++-- mozilla/xpcom/ds/nsString.cpp | 93 ++++++++++++++++++++++ mozilla/xpcom/string/obsolete/nsString.cpp | 93 ++++++++++++++++++++++ 7 files changed, 505 insertions(+), 13 deletions(-) diff --git a/mozilla/base/src/makefile.win b/mozilla/base/src/makefile.win index 61981a8d4f6..56b0a7d4529 100644 --- a/mozilla/base/src/makefile.win +++ b/mozilla/base/src/makefile.win @@ -76,7 +76,7 @@ MODULE=raptor REQUIRES=xpcom netlib raptor uconv LINCS=-I$(PUBLIC)\xpcom -I$(PUBLIC)\netlib \ - -I$(PUBLIC)\raptor -I$(PUBLIC)\uconv + -I$(PUBLIC)\raptor -I$(PUBLIC)\uconv -I$(PUBLIC)\unicharutil MAKE_OBJ_TYPE = DLL DLLNAME = raptorbase diff --git a/mozilla/base/src/nsCRT.cpp b/mozilla/base/src/nsCRT.cpp index a3462df90a4..48a5e991db3 100644 --- a/mozilla/base/src/nsCRT.cpp +++ b/mozilla/base/src/nsCRT.cpp @@ -33,6 +33,10 @@ #include "nsCRT.h" +#include "nsUnicharUtilCIID.h" +#include "nsIServiceManager.h" +#include "nsICaseConversion.h" + // XXX Bug: These tables don't lowercase the upper 128 characters properly @@ -109,21 +113,77 @@ static const PRUnichar kIsoLatin1ToUCS2[256] = { //---------------------------------------------------------------------- #define TOLOWER(_ucs2) \ - (((_ucs2) < 256) ? PRUnichar(kUpper2Lower[_ucs2]) : _ToLower(_ucs2)) + (((_ucs2) < 128) ? PRUnichar(kUpper2Lower[_ucs2]) : _ToLower(_ucs2)) #define TOUPPER(_ucs2) \ - (((_ucs2) < 256) ? PRUnichar(kLower2Upper[_ucs2]) : _ToUpper(_ucs2)) + (((_ucs2) < 128) ? PRUnichar(kLower2Upper[_ucs2]) : _ToUpper(_ucs2)) + +class HandleCaseConversionShutdown : public nsIShutdownListener { +public : + NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); + HandleCaseConversionShutdown(void) { NS_INIT_REFCNT(); } + virtual ~HandleCaseConversionShutdown(void) {} + NS_DECL_ISUPPORTS +}; +static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); +static NS_DEFINE_IID(kICaseConversionIID, NS_ICASECONVERSION_IID); + +static nsICaseConversion * gCaseConv = NULL; + +static NS_DEFINE_IID(kIShutdownListenerIID, NS_ISHUTDOWNLISTENER_IID); +NS_IMPL_ISUPPORTS(HandleCaseConversionShutdown, kIShutdownListenerIID); + +nsresult +HandleCaseConversionShutdown::OnShutdown(const nsCID& cid, nsISupports* service) +{ + if (cid.Equals(kUnicharUtilCID)) { + NS_ASSERTION(service == gCaseConv, "wrong service!"); + nsrefcnt cnt = gCaseConv->Release(); + gCaseConv = NULL; + } + return NS_OK; +} + +static HandleCaseConversionShutdown* gListener = NULL; + +static void StartUpCaseConversion() +{ + nsresult err; + + if ( NULL == gListener ) + { + gListener = new HandleCaseConversionShutdown(); + gListener->AddRef(); + } + err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, + (nsISupports**) &gCaseConv, gListener); +} +static void CheckCaseConversion() +{ + if(NULL == gCaseConv ) + StartUpCaseConversion(); + + NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); + +} static PRUnichar _ToLower(PRUnichar aChar) { - // XXX need i18n code here - return aChar; + PRUnichar oLower; + CheckCaseConversion(); + nsresult err = gCaseConv->ToLower(aChar, &oLower); + NS_ASSERTION( NS_SUCCEEDED(err), "failed to communicate to UnicharUtil"); + return ( NS_SUCCEEDED(err) ) ? oLower : aChar ; } static PRUnichar _ToUpper(PRUnichar aChar) { - // XXX need i18n code here - return aChar; + nsresult err; + PRUnichar oUpper; + CheckCaseConversion(); + err = gCaseConv->ToUpper(aChar, &oUpper); + NS_ASSERTION( NS_SUCCEEDED(err), "failed to communicate to UnicharUtil"); + return ( NS_SUCCEEDED(err) ) ? oUpper : aChar ; } //---------------------------------------------------------------------- diff --git a/mozilla/base/src/nsString.cpp b/mozilla/base/src/nsString.cpp index 1cdd697d754..086adb7730d 100644 --- a/mozilla/base/src/nsString.cpp +++ b/mozilla/base/src/nsString.cpp @@ -27,6 +27,11 @@ #include "prdtoa.h" #include "nsISizeOfHandler.h" + +#include "nsUnicharUtilCIID.h" +#include "nsIServiceManager.h" +#include "nsICaseConversion.h" + const PRInt32 kGrowthDelta = 8; const PRInt32 kNotFound = -1; PRUnichar gBadChar = 0; @@ -96,6 +101,57 @@ public: }; static CTableConstructor gTableConstructor; +//---- XPCOM code to connect with UnicharUtil + +class HandleCaseConversionShutdown2 : public nsIShutdownListener { +public : + NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); + HandleCaseConversionShutdown2(void) { NS_INIT_REFCNT(); } + virtual ~HandleCaseConversionShutdown2(void) {} + NS_DECL_ISUPPORTS +}; +static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); +static NS_DEFINE_IID(kICaseConversionIID, NS_ICASECONVERSION_IID); + +static nsICaseConversion * gCaseConv = NULL; + +static NS_DEFINE_IID(kIShutdownListenerIID, NS_ISHUTDOWNLISTENER_IID); +NS_IMPL_ISUPPORTS(HandleCaseConversionShutdown2, kIShutdownListenerIID); + +nsresult +HandleCaseConversionShutdown2::OnShutdown(const nsCID& cid, nsISupports* service) +{ + if (cid.Equals(kUnicharUtilCID)) { + NS_ASSERTION(service == gCaseConv, "wrong service!"); + nsrefcnt cnt = gCaseConv->Release(); + gCaseConv = NULL; + } + return NS_OK; +} + +static HandleCaseConversionShutdown2* gListener = NULL; + +static void StartUpCaseConversion() +{ + nsresult err; + + if ( NULL == gListener ) + { + gListener = new HandleCaseConversionShutdown2(); + gListener->AddRef(); + } + err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, + (nsISupports**) &gCaseConv, gListener); +} +static void CheckCaseConversion() +{ + if(NULL == gCaseConv ) + StartUpCaseConversion(); + + NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); + +} + /*********************************************************************** IMPLEMENTATION NOTES: @@ -431,6 +487,14 @@ nsString nsString::operator+(PRUnichar aChar) { */ void nsString::ToLowerCase() { + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToLower(mStr, mStr, mLength); + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* cp = mStr; chartype* end = cp + mLength; while (cp < end) { @@ -448,6 +512,14 @@ void nsString::ToLowerCase() */ void nsString::ToUpperCase() { + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToUpper(mStr, mStr, mLength); + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* cp = mStr; chartype* end = cp + mLength; while (cp < end) { @@ -487,6 +559,17 @@ void nsString::ToLowerCase(nsString& aOut) const { aOut.EnsureCapacityFor(mLength); aOut.mLength = mLength; + + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToLower(mStr, aOut.mStr, mLength); + (*(aOut.mStr+mLength)) = 0; + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code + chartype* to = aOut.mStr; chartype* from = mStr; chartype* end = from + mLength; @@ -510,6 +593,16 @@ void nsString::ToUpperCase(nsString& aOut) const { aOut.EnsureCapacityFor(mLength); aOut.mLength = mLength; + + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToUpper(mStr, aOut.mStr, mLength); + (*(aOut.mStr+mLength)) = 0; + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* to = aOut.mStr; chartype* from = mStr; chartype* end = from + mLength; diff --git a/mozilla/string/obsolete/nsString.cpp b/mozilla/string/obsolete/nsString.cpp index 1cdd697d754..086adb7730d 100644 --- a/mozilla/string/obsolete/nsString.cpp +++ b/mozilla/string/obsolete/nsString.cpp @@ -27,6 +27,11 @@ #include "prdtoa.h" #include "nsISizeOfHandler.h" + +#include "nsUnicharUtilCIID.h" +#include "nsIServiceManager.h" +#include "nsICaseConversion.h" + const PRInt32 kGrowthDelta = 8; const PRInt32 kNotFound = -1; PRUnichar gBadChar = 0; @@ -96,6 +101,57 @@ public: }; static CTableConstructor gTableConstructor; +//---- XPCOM code to connect with UnicharUtil + +class HandleCaseConversionShutdown2 : public nsIShutdownListener { +public : + NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); + HandleCaseConversionShutdown2(void) { NS_INIT_REFCNT(); } + virtual ~HandleCaseConversionShutdown2(void) {} + NS_DECL_ISUPPORTS +}; +static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); +static NS_DEFINE_IID(kICaseConversionIID, NS_ICASECONVERSION_IID); + +static nsICaseConversion * gCaseConv = NULL; + +static NS_DEFINE_IID(kIShutdownListenerIID, NS_ISHUTDOWNLISTENER_IID); +NS_IMPL_ISUPPORTS(HandleCaseConversionShutdown2, kIShutdownListenerIID); + +nsresult +HandleCaseConversionShutdown2::OnShutdown(const nsCID& cid, nsISupports* service) +{ + if (cid.Equals(kUnicharUtilCID)) { + NS_ASSERTION(service == gCaseConv, "wrong service!"); + nsrefcnt cnt = gCaseConv->Release(); + gCaseConv = NULL; + } + return NS_OK; +} + +static HandleCaseConversionShutdown2* gListener = NULL; + +static void StartUpCaseConversion() +{ + nsresult err; + + if ( NULL == gListener ) + { + gListener = new HandleCaseConversionShutdown2(); + gListener->AddRef(); + } + err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, + (nsISupports**) &gCaseConv, gListener); +} +static void CheckCaseConversion() +{ + if(NULL == gCaseConv ) + StartUpCaseConversion(); + + NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); + +} + /*********************************************************************** IMPLEMENTATION NOTES: @@ -431,6 +487,14 @@ nsString nsString::operator+(PRUnichar aChar) { */ void nsString::ToLowerCase() { + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToLower(mStr, mStr, mLength); + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* cp = mStr; chartype* end = cp + mLength; while (cp < end) { @@ -448,6 +512,14 @@ void nsString::ToLowerCase() */ void nsString::ToUpperCase() { + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToUpper(mStr, mStr, mLength); + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* cp = mStr; chartype* end = cp + mLength; while (cp < end) { @@ -487,6 +559,17 @@ void nsString::ToLowerCase(nsString& aOut) const { aOut.EnsureCapacityFor(mLength); aOut.mLength = mLength; + + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToLower(mStr, aOut.mStr, mLength); + (*(aOut.mStr+mLength)) = 0; + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code + chartype* to = aOut.mStr; chartype* from = mStr; chartype* end = from + mLength; @@ -510,6 +593,16 @@ void nsString::ToUpperCase(nsString& aOut) const { aOut.EnsureCapacityFor(mLength); aOut.mLength = mLength; + + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToUpper(mStr, aOut.mStr, mLength); + (*(aOut.mStr+mLength)) = 0; + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* to = aOut.mStr; chartype* from = mStr; chartype* end = from + mLength; diff --git a/mozilla/xpcom/ds/nsCRT.cpp b/mozilla/xpcom/ds/nsCRT.cpp index a3462df90a4..48a5e991db3 100644 --- a/mozilla/xpcom/ds/nsCRT.cpp +++ b/mozilla/xpcom/ds/nsCRT.cpp @@ -33,6 +33,10 @@ #include "nsCRT.h" +#include "nsUnicharUtilCIID.h" +#include "nsIServiceManager.h" +#include "nsICaseConversion.h" + // XXX Bug: These tables don't lowercase the upper 128 characters properly @@ -109,21 +113,77 @@ static const PRUnichar kIsoLatin1ToUCS2[256] = { //---------------------------------------------------------------------- #define TOLOWER(_ucs2) \ - (((_ucs2) < 256) ? PRUnichar(kUpper2Lower[_ucs2]) : _ToLower(_ucs2)) + (((_ucs2) < 128) ? PRUnichar(kUpper2Lower[_ucs2]) : _ToLower(_ucs2)) #define TOUPPER(_ucs2) \ - (((_ucs2) < 256) ? PRUnichar(kLower2Upper[_ucs2]) : _ToUpper(_ucs2)) + (((_ucs2) < 128) ? PRUnichar(kLower2Upper[_ucs2]) : _ToUpper(_ucs2)) + +class HandleCaseConversionShutdown : public nsIShutdownListener { +public : + NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); + HandleCaseConversionShutdown(void) { NS_INIT_REFCNT(); } + virtual ~HandleCaseConversionShutdown(void) {} + NS_DECL_ISUPPORTS +}; +static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); +static NS_DEFINE_IID(kICaseConversionIID, NS_ICASECONVERSION_IID); + +static nsICaseConversion * gCaseConv = NULL; + +static NS_DEFINE_IID(kIShutdownListenerIID, NS_ISHUTDOWNLISTENER_IID); +NS_IMPL_ISUPPORTS(HandleCaseConversionShutdown, kIShutdownListenerIID); + +nsresult +HandleCaseConversionShutdown::OnShutdown(const nsCID& cid, nsISupports* service) +{ + if (cid.Equals(kUnicharUtilCID)) { + NS_ASSERTION(service == gCaseConv, "wrong service!"); + nsrefcnt cnt = gCaseConv->Release(); + gCaseConv = NULL; + } + return NS_OK; +} + +static HandleCaseConversionShutdown* gListener = NULL; + +static void StartUpCaseConversion() +{ + nsresult err; + + if ( NULL == gListener ) + { + gListener = new HandleCaseConversionShutdown(); + gListener->AddRef(); + } + err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, + (nsISupports**) &gCaseConv, gListener); +} +static void CheckCaseConversion() +{ + if(NULL == gCaseConv ) + StartUpCaseConversion(); + + NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); + +} static PRUnichar _ToLower(PRUnichar aChar) { - // XXX need i18n code here - return aChar; + PRUnichar oLower; + CheckCaseConversion(); + nsresult err = gCaseConv->ToLower(aChar, &oLower); + NS_ASSERTION( NS_SUCCEEDED(err), "failed to communicate to UnicharUtil"); + return ( NS_SUCCEEDED(err) ) ? oLower : aChar ; } static PRUnichar _ToUpper(PRUnichar aChar) { - // XXX need i18n code here - return aChar; + nsresult err; + PRUnichar oUpper; + CheckCaseConversion(); + err = gCaseConv->ToUpper(aChar, &oUpper); + NS_ASSERTION( NS_SUCCEEDED(err), "failed to communicate to UnicharUtil"); + return ( NS_SUCCEEDED(err) ) ? oUpper : aChar ; } //---------------------------------------------------------------------- diff --git a/mozilla/xpcom/ds/nsString.cpp b/mozilla/xpcom/ds/nsString.cpp index 1cdd697d754..086adb7730d 100644 --- a/mozilla/xpcom/ds/nsString.cpp +++ b/mozilla/xpcom/ds/nsString.cpp @@ -27,6 +27,11 @@ #include "prdtoa.h" #include "nsISizeOfHandler.h" + +#include "nsUnicharUtilCIID.h" +#include "nsIServiceManager.h" +#include "nsICaseConversion.h" + const PRInt32 kGrowthDelta = 8; const PRInt32 kNotFound = -1; PRUnichar gBadChar = 0; @@ -96,6 +101,57 @@ public: }; static CTableConstructor gTableConstructor; +//---- XPCOM code to connect with UnicharUtil + +class HandleCaseConversionShutdown2 : public nsIShutdownListener { +public : + NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); + HandleCaseConversionShutdown2(void) { NS_INIT_REFCNT(); } + virtual ~HandleCaseConversionShutdown2(void) {} + NS_DECL_ISUPPORTS +}; +static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); +static NS_DEFINE_IID(kICaseConversionIID, NS_ICASECONVERSION_IID); + +static nsICaseConversion * gCaseConv = NULL; + +static NS_DEFINE_IID(kIShutdownListenerIID, NS_ISHUTDOWNLISTENER_IID); +NS_IMPL_ISUPPORTS(HandleCaseConversionShutdown2, kIShutdownListenerIID); + +nsresult +HandleCaseConversionShutdown2::OnShutdown(const nsCID& cid, nsISupports* service) +{ + if (cid.Equals(kUnicharUtilCID)) { + NS_ASSERTION(service == gCaseConv, "wrong service!"); + nsrefcnt cnt = gCaseConv->Release(); + gCaseConv = NULL; + } + return NS_OK; +} + +static HandleCaseConversionShutdown2* gListener = NULL; + +static void StartUpCaseConversion() +{ + nsresult err; + + if ( NULL == gListener ) + { + gListener = new HandleCaseConversionShutdown2(); + gListener->AddRef(); + } + err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, + (nsISupports**) &gCaseConv, gListener); +} +static void CheckCaseConversion() +{ + if(NULL == gCaseConv ) + StartUpCaseConversion(); + + NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); + +} + /*********************************************************************** IMPLEMENTATION NOTES: @@ -431,6 +487,14 @@ nsString nsString::operator+(PRUnichar aChar) { */ void nsString::ToLowerCase() { + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToLower(mStr, mStr, mLength); + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* cp = mStr; chartype* end = cp + mLength; while (cp < end) { @@ -448,6 +512,14 @@ void nsString::ToLowerCase() */ void nsString::ToUpperCase() { + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToUpper(mStr, mStr, mLength); + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* cp = mStr; chartype* end = cp + mLength; while (cp < end) { @@ -487,6 +559,17 @@ void nsString::ToLowerCase(nsString& aOut) const { aOut.EnsureCapacityFor(mLength); aOut.mLength = mLength; + + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToLower(mStr, aOut.mStr, mLength); + (*(aOut.mStr+mLength)) = 0; + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code + chartype* to = aOut.mStr; chartype* from = mStr; chartype* end = from + mLength; @@ -510,6 +593,16 @@ void nsString::ToUpperCase(nsString& aOut) const { aOut.EnsureCapacityFor(mLength); aOut.mLength = mLength; + + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToUpper(mStr, aOut.mStr, mLength); + (*(aOut.mStr+mLength)) = 0; + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* to = aOut.mStr; chartype* from = mStr; chartype* end = from + mLength; diff --git a/mozilla/xpcom/string/obsolete/nsString.cpp b/mozilla/xpcom/string/obsolete/nsString.cpp index 1cdd697d754..086adb7730d 100644 --- a/mozilla/xpcom/string/obsolete/nsString.cpp +++ b/mozilla/xpcom/string/obsolete/nsString.cpp @@ -27,6 +27,11 @@ #include "prdtoa.h" #include "nsISizeOfHandler.h" + +#include "nsUnicharUtilCIID.h" +#include "nsIServiceManager.h" +#include "nsICaseConversion.h" + const PRInt32 kGrowthDelta = 8; const PRInt32 kNotFound = -1; PRUnichar gBadChar = 0; @@ -96,6 +101,57 @@ public: }; static CTableConstructor gTableConstructor; +//---- XPCOM code to connect with UnicharUtil + +class HandleCaseConversionShutdown2 : public nsIShutdownListener { +public : + NS_IMETHOD OnShutdown(const nsCID& cid, nsISupports* service); + HandleCaseConversionShutdown2(void) { NS_INIT_REFCNT(); } + virtual ~HandleCaseConversionShutdown2(void) {} + NS_DECL_ISUPPORTS +}; +static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID); +static NS_DEFINE_IID(kICaseConversionIID, NS_ICASECONVERSION_IID); + +static nsICaseConversion * gCaseConv = NULL; + +static NS_DEFINE_IID(kIShutdownListenerIID, NS_ISHUTDOWNLISTENER_IID); +NS_IMPL_ISUPPORTS(HandleCaseConversionShutdown2, kIShutdownListenerIID); + +nsresult +HandleCaseConversionShutdown2::OnShutdown(const nsCID& cid, nsISupports* service) +{ + if (cid.Equals(kUnicharUtilCID)) { + NS_ASSERTION(service == gCaseConv, "wrong service!"); + nsrefcnt cnt = gCaseConv->Release(); + gCaseConv = NULL; + } + return NS_OK; +} + +static HandleCaseConversionShutdown2* gListener = NULL; + +static void StartUpCaseConversion() +{ + nsresult err; + + if ( NULL == gListener ) + { + gListener = new HandleCaseConversionShutdown2(); + gListener->AddRef(); + } + err = nsServiceManager::GetService(kUnicharUtilCID, kICaseConversionIID, + (nsISupports**) &gCaseConv, gListener); +} +static void CheckCaseConversion() +{ + if(NULL == gCaseConv ) + StartUpCaseConversion(); + + NS_ASSERTION( gCaseConv != NULL , "cannot obtain UnicharUtil"); + +} + /*********************************************************************** IMPLEMENTATION NOTES: @@ -431,6 +487,14 @@ nsString nsString::operator+(PRUnichar aChar) { */ void nsString::ToLowerCase() { + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToLower(mStr, mStr, mLength); + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* cp = mStr; chartype* end = cp + mLength; while (cp < end) { @@ -448,6 +512,14 @@ void nsString::ToLowerCase() */ void nsString::ToUpperCase() { + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToUpper(mStr, mStr, mLength); + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* cp = mStr; chartype* end = cp + mLength; while (cp < end) { @@ -487,6 +559,17 @@ void nsString::ToLowerCase(nsString& aOut) const { aOut.EnsureCapacityFor(mLength); aOut.mLength = mLength; + + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToLower(mStr, aOut.mStr, mLength); + (*(aOut.mStr+mLength)) = 0; + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code + chartype* to = aOut.mStr; chartype* from = mStr; chartype* end = from + mLength; @@ -510,6 +593,16 @@ void nsString::ToUpperCase(nsString& aOut) const { aOut.EnsureCapacityFor(mLength); aOut.mLength = mLength; + + // I18N code begin + CheckCaseConversion(); + nsresult err = gCaseConv->ToUpper(mStr, aOut.mStr, mLength); + (*(aOut.mStr+mLength)) = 0; + if( NS_SUCCEEDED(err)) + return; + // I18N code end + + // somehow UnicharUtil return failed, fallback to the old ascii only code chartype* to = aOut.mStr; chartype* from = mStr; chartype* end = from + mLength;