Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "mozilla/ArrayUtils.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsCharsetAlias.h" |
michael@0 | 9 | |
michael@0 | 10 | // for NS_ERROR_UCONV_NOCONV |
michael@0 | 11 | #include "nsCharsetConverterManager.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "nsUConvPropertySearch.h" |
michael@0 | 14 | |
michael@0 | 15 | using namespace mozilla; |
michael@0 | 16 | |
michael@0 | 17 | // |
michael@0 | 18 | static const char* kAliases[][3] = { |
michael@0 | 19 | #include "charsetalias.properties.h" |
michael@0 | 20 | }; |
michael@0 | 21 | |
michael@0 | 22 | //-------------------------------------------------------------- |
michael@0 | 23 | // static |
michael@0 | 24 | nsresult |
michael@0 | 25 | nsCharsetAlias::GetPreferredInternal(const nsACString& aAlias, |
michael@0 | 26 | nsACString& oResult) |
michael@0 | 27 | { |
michael@0 | 28 | nsAutoCString key(aAlias); |
michael@0 | 29 | ToLowerCase(key); |
michael@0 | 30 | |
michael@0 | 31 | return nsUConvPropertySearch::SearchPropertyValue(kAliases, |
michael@0 | 32 | ArrayLength(kAliases), key, oResult); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | //-------------------------------------------------------------- |
michael@0 | 36 | // static |
michael@0 | 37 | nsresult |
michael@0 | 38 | nsCharsetAlias::GetPreferred(const nsACString& aAlias, |
michael@0 | 39 | nsACString& oResult) |
michael@0 | 40 | { |
michael@0 | 41 | if (aAlias.IsEmpty()) return NS_ERROR_NULL_POINTER; |
michael@0 | 42 | |
michael@0 | 43 | nsresult res = GetPreferredInternal(aAlias, oResult); |
michael@0 | 44 | if (NS_FAILED(res)) |
michael@0 | 45 | return res; |
michael@0 | 46 | |
michael@0 | 47 | if (nsCharsetConverterManager::IsInternal(oResult)) |
michael@0 | 48 | return NS_ERROR_UCONV_NOCONV; |
michael@0 | 49 | |
michael@0 | 50 | return res; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | //-------------------------------------------------------------- |
michael@0 | 54 | // static |
michael@0 | 55 | nsresult |
michael@0 | 56 | nsCharsetAlias::Equals(const nsACString& aCharset1, |
michael@0 | 57 | const nsACString& aCharset2, bool* oResult) |
michael@0 | 58 | { |
michael@0 | 59 | nsresult res = NS_OK; |
michael@0 | 60 | |
michael@0 | 61 | if(aCharset1.Equals(aCharset2, nsCaseInsensitiveCStringComparator())) { |
michael@0 | 62 | *oResult = true; |
michael@0 | 63 | return res; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | if(aCharset1.IsEmpty() || aCharset2.IsEmpty()) { |
michael@0 | 67 | *oResult = false; |
michael@0 | 68 | return res; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | *oResult = false; |
michael@0 | 72 | nsAutoCString name1; |
michael@0 | 73 | res = GetPreferredInternal(aCharset1, name1); |
michael@0 | 74 | if (NS_FAILED(res)) |
michael@0 | 75 | return res; |
michael@0 | 76 | |
michael@0 | 77 | nsAutoCString name2; |
michael@0 | 78 | res = GetPreferredInternal(aCharset2, name2); |
michael@0 | 79 | if (NS_FAILED(res)) |
michael@0 | 80 | return res; |
michael@0 | 81 | |
michael@0 | 82 | *oResult = name1.Equals(name2); |
michael@0 | 83 | return NS_OK; |
michael@0 | 84 | } |