From cee5e4461c4063bc89cb5af0b50de743adc3583f Mon Sep 17 00:00:00 2001 From: "nhotta%netscape.com" Date: Thu, 22 Feb 2001 23:27:53 +0000 Subject: [PATCH] Changed to cache unicode decoder, bug 68826, r=yokoyama, sr=erik. --- intl/locale/src/mac/nsDateTimeFormatMac.cpp | 55 +++++++++---------- intl/locale/src/mac/nsDateTimeFormatMac.h | 4 ++ intl/locale/src/unix/nsDateTimeFormatUnix.cpp | 43 +++++++-------- intl/locale/src/unix/nsDateTimeFormatUnix.h | 4 ++ 4 files changed, 56 insertions(+), 50 deletions(-) diff --git a/intl/locale/src/mac/nsDateTimeFormatMac.cpp b/intl/locale/src/mac/nsDateTimeFormatMac.cpp index 070bdd3b196f..ba7d66b579e6 100644 --- a/intl/locale/src/mac/nsDateTimeFormatMac.cpp +++ b/intl/locale/src/mac/nsDateTimeFormatMac.cpp @@ -22,7 +22,6 @@ */ #include "nsIServiceManager.h" -#include "nsICharsetConverterManager.h" #include "nsDateTimeFormatMac.h" #include #include @@ -35,7 +34,6 @@ #include "nsIPlatformCharset.h" #include "nsIMacLocale.h" #include "nsCRT.h" -#include "nsCOMPtr.h" #include "plstr.h" #include "prmem.h" @@ -301,6 +299,18 @@ nsresult nsDateTimeFormatMac::Initialize(nsILocale* locale) res = platformCharset->GetCharset(kPlatformCharsetSel_FileName, mSystemCharset); } } + + // Initialize unicode decoder + nsCOMPtr charsetAtom; + nsCOMPtr charsetConverterManager; + charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res); + if (NS_SUCCEEDED(res)) { + res = charsetConverterManager->GetCharsetAtom(mUseDefaultLocale ? mSystemCharset.GetUnicode() : mCharset.GetUnicode(), + getter_AddRefs(charsetAtom)); + if (NS_SUCCEEDED(res)) { + res = charsetConverterManager->GetUnicodeDecoder(charsetAtom, getter_AddRefs(mDecoder)); + } + } return res; } @@ -403,40 +413,29 @@ nsresult nsDateTimeFormatMac::FormatTMTime(nsILocale* locale, } // construct a C string - char *aBuffer; + char *localBuffer; if (dateFormatSelector != kDateFormatNone && timeFormatSelector != kTimeFormatNone) { - aBuffer = p2cstr(dateString); - strcat(aBuffer, " "); - strcat(aBuffer, p2cstr(timeString)); + localBuffer = p2cstr(dateString); + strcat(localBuffer, " "); + strcat(localBuffer, p2cstr(timeString)); } else if (dateFormatSelector != kDateFormatNone) { - aBuffer = p2cstr(dateString); + localBuffer = p2cstr(dateString); } else if (timeFormatSelector != kTimeFormatNone) { - aBuffer = p2cstr(timeString); + localBuffer = p2cstr(timeString); } - // convert result to unicode - NS_WITH_SERVICE(nsICharsetConverterManager, ccm, kCharsetConverterManagerCID, &res); - if(NS_SUCCEEDED(res) && ccm) { - nsCOMPtr decoder; - res = ccm->GetUnicodeDecoder(mUseDefaultLocale ? &mSystemCharset : &mCharset, getter_AddRefs(decoder)); - if(NS_SUCCEEDED(res) && decoder) { - PRInt32 unicharLength = 0; - PRInt32 srcLength = (PRInt32) PL_strlen(aBuffer); - res = decoder->GetMaxLength(aBuffer, srcLength, &unicharLength); - PRUnichar *unichars = new PRUnichar [ unicharLength ]; - - if (nsnull != unichars) { - res = decoder->Convert(aBuffer, &srcLength, - unichars, &unicharLength); - if (NS_SUCCEEDED(res)) { - stringOut.Assign(unichars, unicharLength); - } - } - delete [] unichars; - } + if (mDecoder) { + PRInt32 srcLength = (PRInt32) PL_strlen(localBuffer); + PRInt32 unicharLength = sizeof(Str255)*2; + PRUnichar unichars[sizeof(Str255)*2]; // buffer for combined date and time + + res = mDecoder->Convert(localBuffer, &srcLength, unichars, &unicharLength); + if (NS_SUCCEEDED(res)) { + stringOut.Assign(unichars, unicharLength); + } } return res; diff --git a/intl/locale/src/mac/nsDateTimeFormatMac.h b/intl/locale/src/mac/nsDateTimeFormatMac.h index f8949ca7eac9..8b4257ad4de6 100644 --- a/intl/locale/src/mac/nsDateTimeFormatMac.h +++ b/intl/locale/src/mac/nsDateTimeFormatMac.h @@ -24,6 +24,9 @@ #define nsDateTimeFormatMac_h__ +#include "nsICharsetConverterManager.h" +#include "nsICharsetConverterManager2.h" +#include "nsCOMPtr.h" #include "nsIDateTimeFormat.h" @@ -75,6 +78,7 @@ private: short mLangcode; short mRegioncode; bool mUseDefaultLocale; + nsCOMPtr mDecoder; }; #endif /* nsDateTimeFormatMac_h__ */ diff --git a/intl/locale/src/unix/nsDateTimeFormatUnix.cpp b/intl/locale/src/unix/nsDateTimeFormatUnix.cpp index 9f82a86ca138..0b1c22518051 100644 --- a/intl/locale/src/unix/nsDateTimeFormatUnix.cpp +++ b/intl/locale/src/unix/nsDateTimeFormatUnix.cpp @@ -24,14 +24,12 @@ #include #include "plstr.h" #include "nsIServiceManager.h" -#include "nsICharsetConverterManager.h" #include "nsDateTimeFormatUnix.h" #include "nsIComponentManager.h" #include "nsLocaleCID.h" #include "nsILocaleService.h" #include "nsIPlatformCharset.h" #include "nsIPosixLocale.h" -#include "nsCOMPtr.h" #include "nsCRT.h" static NS_DEFINE_IID(kIDateTimeFormatIID, NS_IDATETIMEFORMAT_IID); @@ -110,6 +108,17 @@ nsresult nsDateTimeFormatUnix::Initialize(nsILocale* locale) } } + // Initialize unicode decoder + nsCOMPtr charsetAtom; + nsCOMPtr charsetConverterManager; + charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res); + if (NS_SUCCEEDED(res)) { + res = charsetConverterManager->GetCharsetAtom(mCharset.GetUnicode(), getter_AddRefs(charsetAtom)); + if (NS_SUCCEEDED(res)) { + res = charsetConverterManager->GetUnicodeDecoder(charsetAtom, getter_AddRefs(mDecoder)); + } + } + mLocalePreferred24hour = LocalePreferred24hour(); return res; @@ -158,7 +167,7 @@ nsresult nsDateTimeFormatUnix::FormatTMTime(nsILocale* locale, nsString& stringOut) { #define NSDATETIME_FORMAT_BUFFER_LEN 80 - char strOut[NSDATETIME_FORMAT_BUFFER_LEN]; + char strOut[NSDATETIME_FORMAT_BUFFER_LEN*2]; // buffer for date and time char fmtD[NSDATETIME_FORMAT_BUFFER_LEN], fmtT[NSDATETIME_FORMAT_BUFFER_LEN]; nsresult res; @@ -232,25 +241,15 @@ nsresult nsDateTimeFormatUnix::FormatTMTime(nsILocale* locale, (void) setlocale(LC_TIME, old_locale); // convert result to unicode - NS_WITH_SERVICE(nsICharsetConverterManager, ccm, kCharsetConverterManagerCID, &res); - if(NS_SUCCEEDED(res) && ccm) { - nsCOMPtr decoder; - res = ccm->GetUnicodeDecoder(&mCharset, getter_AddRefs(decoder)); - if (NS_SUCCEEDED(res) && decoder) { - PRInt32 unicharLength = 0; - PRInt32 srcLength = (PRInt32) PL_strlen(strOut); - res = decoder->GetMaxLength(strOut, srcLength, &unicharLength); - PRUnichar *unichars = new PRUnichar [ unicharLength ]; - - if (nsnull != unichars) { - res = decoder->Convert(strOut, &srcLength, - unichars, &unicharLength); - if (NS_SUCCEEDED(res)) { - stringOut.Assign(unichars, unicharLength); - } - } - delete [] unichars; - } + if (mDecoder) { + PRInt32 srcLength = (PRInt32) PL_strlen(strOut); + PRInt32 unicharLength = NSDATETIME_FORMAT_BUFFER_LEN*2; + PRUnichar unichars[NSDATETIME_FORMAT_BUFFER_LEN*2]; // buffer for date and time + + res = mDecoder->Convert(strOut, &srcLength, unichars, &unicharLength); + if (NS_SUCCEEDED(res)) { + stringOut.Assign(unichars, unicharLength); + } } return res; diff --git a/intl/locale/src/unix/nsDateTimeFormatUnix.h b/intl/locale/src/unix/nsDateTimeFormatUnix.h index 63f1261e9937..e1c6150f8e31 100644 --- a/intl/locale/src/unix/nsDateTimeFormatUnix.h +++ b/intl/locale/src/unix/nsDateTimeFormatUnix.h @@ -24,6 +24,9 @@ #define nsDateTimeFormatUnix_h__ +#include "nsICharsetConverterManager.h" +#include "nsICharsetConverterManager2.h" +#include "nsCOMPtr.h" #include "nsIDateTimeFormat.h" #define kPlatformLocaleLength 64 @@ -78,6 +81,7 @@ private: nsString mCharset; // in order to convert API result to unicode char mPlatformLocale[kPlatformLocaleLength+1]; // for setlocale PRBool mLocalePreferred24hour; // true if 24 hour format is preferred by current locale + nsCOMPtr mDecoder; }; #endif /* nsDateTimeFormatUnix_h__ */