1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsISupports.h"
13 #define LOCALE_HASH_SIZE 0xFF
17 NS_IMPL_ISUPPORTS(nsLocale
, nsILocale
)
19 nsLocale::nsLocale(void)
20 : fHashtable(nullptr), fCategoryCount(0)
22 fHashtable
= PL_NewHashTable(LOCALE_HASH_SIZE
,&nsLocale::Hash_HashFunction
,
23 &nsLocale::Hash_CompareNSString
,
24 &nsLocale::Hash_CompareNSString
,
26 NS_ASSERTION(fHashtable
, "nsLocale: failed to allocate PR_Hashtable");
29 nsLocale::~nsLocale(void)
31 // enumerate all the entries with a delete function to
32 // safely delete all the keys and values
33 PL_HashTableEnumerateEntries(fHashtable
, &nsLocale::Hash_EnumerateDelete
,
36 PL_HashTableDestroy(fHashtable
);
40 nsLocale::GetCategory(const nsAString
& category
, nsAString
& result
)
42 const char16_t
*value
= (const char16_t
*)
43 PL_HashTableLookup(fHashtable
, PromiseFlatString(category
).get());
51 return NS_ERROR_FAILURE
;
55 nsLocale::AddCategory(const nsAString
&category
, const nsAString
&value
)
57 char16_t
* newKey
= ToNewUnicode(category
);
59 return NS_ERROR_OUT_OF_MEMORY
;
61 char16_t
* newValue
= ToNewUnicode(value
);
63 nsMemory::Free(newKey
);
64 return NS_ERROR_OUT_OF_MEMORY
;
67 if (!PL_HashTableAdd(fHashtable
, newKey
, newValue
)) {
68 nsMemory::Free(newKey
);
69 nsMemory::Free(newValue
);
70 return NS_ERROR_OUT_OF_MEMORY
;
78 nsLocale::Hash_HashFunction(const void* key
)
80 const char16_t
* ptr
= (const char16_t
*) key
;
83 hash
= (PLHashNumber
)0;
86 hash
+= (PLHashNumber
) *ptr
++;
93 nsLocale::Hash_CompareNSString(const void* s1
, const void* s2
)
95 return !nsCRT::strcmp((const char16_t
*) s1
, (const char16_t
*) s2
);
100 nsLocale::Hash_EnumerateDelete(PLHashEntry
*he
, int hashIndex
, void *arg
)
103 nsMemory::Free((char16_t
*)he
->key
);
104 nsMemory::Free((char16_t
*)he
->value
);
106 return (HT_ENUMERATE_NEXT
| HT_ENUMERATE_REMOVE
);