Bumping gaia.json for 4 gaia revision(s) a=gaia-bump
[gecko.git] / intl / locale / nsLocale.cpp
blobe113dc587be1848af24858d05aa5e267db92024f
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/. */
6 #include "nsString.h"
7 #include "nsISupports.h"
8 #include "nsILocale.h"
9 #include "nsLocale.h"
10 #include "nsMemory.h"
11 #include "nsCRT.h"
13 #define LOCALE_HASH_SIZE 0xFF
16 /* nsILocale */
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,
25 nullptr, nullptr);
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,
34 nullptr);
36 PL_HashTableDestroy(fHashtable);
39 NS_IMETHODIMP
40 nsLocale::GetCategory(const nsAString& category, nsAString& result)
42 const char16_t *value = (const char16_t*)
43 PL_HashTableLookup(fHashtable, PromiseFlatString(category).get());
45 if (value)
47 result.Assign(value);
48 return NS_OK;
51 return NS_ERROR_FAILURE;
54 NS_IMETHODIMP
55 nsLocale::AddCategory(const nsAString &category, const nsAString &value)
57 char16_t* newKey = ToNewUnicode(category);
58 if (!newKey)
59 return NS_ERROR_OUT_OF_MEMORY;
61 char16_t* newValue = ToNewUnicode(value);
62 if (!newValue) {
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;
73 return NS_OK;
77 PLHashNumber
78 nsLocale::Hash_HashFunction(const void* key)
80 const char16_t* ptr = (const char16_t *) key;
81 PLHashNumber hash;
83 hash = (PLHashNumber)0;
85 while (*ptr)
86 hash += (PLHashNumber) *ptr++;
88 return hash;
92 int
93 nsLocale::Hash_CompareNSString(const void* s1, const void* s2)
95 return !nsCRT::strcmp((const char16_t *) s1, (const char16_t *) s2);
99 int
100 nsLocale::Hash_EnumerateDelete(PLHashEntry *he, int hashIndex, void *arg)
102 // delete an entry
103 nsMemory::Free((char16_t *)he->key);
104 nsMemory::Free((char16_t *)he->value);
106 return (HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE);