EncodingUtils.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* -*- Mode: C++; tab-width: 8; 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/. */
  5. #include "mozilla/dom/EncodingUtils.h"
  6. #include "mozilla/ArrayUtils.h" // ArrayLength
  7. #include "nsUConvPropertySearch.h"
  8. #include "nsIUnicodeDecoder.h"
  9. #include "nsIUnicodeEncoder.h"
  10. #include "nsComponentManagerUtils.h"
  11. namespace mozilla {
  12. namespace dom {
  13. static const nsUConvProp labelsEncodings[] = {
  14. #include "labelsencodings.properties.h"
  15. };
  16. static const nsUConvProp encodingsGroups[] = {
  17. #include "encodingsgroups.properties.h"
  18. };
  19. bool
  20. EncodingUtils::FindEncodingForLabel(const nsACString& aLabel,
  21. nsACString& aOutEncoding)
  22. {
  23. // Save aLabel first because it may refer the same string as aOutEncoding.
  24. nsCString label(aLabel);
  25. EncodingUtils::TrimSpaceCharacters(label);
  26. if (label.IsEmpty()) {
  27. aOutEncoding.Truncate();
  28. return false;
  29. }
  30. ToLowerCase(label);
  31. return NS_SUCCEEDED(nsUConvPropertySearch::SearchPropertyValue(
  32. labelsEncodings, ArrayLength(labelsEncodings), label, aOutEncoding));
  33. }
  34. bool
  35. EncodingUtils::FindEncodingForLabelNoReplacement(const nsACString& aLabel,
  36. nsACString& aOutEncoding)
  37. {
  38. if(!FindEncodingForLabel(aLabel, aOutEncoding)) {
  39. return false;
  40. }
  41. if (aOutEncoding.EqualsLiteral("replacement")) {
  42. aOutEncoding.Truncate();
  43. return false;
  44. }
  45. return true;
  46. }
  47. bool
  48. EncodingUtils::IsAsciiCompatible(const nsACString& aPreferredName)
  49. {
  50. // HZ and UTF-7 are no longer in mozilla-central, but keeping them here
  51. // just in case for the benefit of comm-central.
  52. return !(aPreferredName.LowerCaseEqualsLiteral("utf-16") ||
  53. aPreferredName.LowerCaseEqualsLiteral("utf-16be") ||
  54. aPreferredName.LowerCaseEqualsLiteral("utf-16le") ||
  55. aPreferredName.LowerCaseEqualsLiteral("replacement") ||
  56. aPreferredName.LowerCaseEqualsLiteral("hz-gb-2312") ||
  57. aPreferredName.LowerCaseEqualsLiteral("utf-7") ||
  58. aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7"));
  59. }
  60. already_AddRefed<nsIUnicodeDecoder>
  61. EncodingUtils::DecoderForEncoding(const nsACString& aEncoding)
  62. {
  63. nsAutoCString contractId(NS_UNICODEDECODER_CONTRACTID_BASE);
  64. contractId.Append(aEncoding);
  65. nsCOMPtr<nsIUnicodeDecoder> decoder = do_CreateInstance(contractId.get());
  66. MOZ_ASSERT(decoder, "Tried to create decoder for unknown encoding.");
  67. return decoder.forget();
  68. }
  69. already_AddRefed<nsIUnicodeEncoder>
  70. EncodingUtils::EncoderForEncoding(const nsACString& aEncoding)
  71. {
  72. nsAutoCString contractId(NS_UNICODEENCODER_CONTRACTID_BASE);
  73. contractId.Append(aEncoding);
  74. nsCOMPtr<nsIUnicodeEncoder> encoder = do_CreateInstance(contractId.get());
  75. MOZ_ASSERT(encoder, "Tried to create encoder for unknown encoding.");
  76. return encoder.forget();
  77. }
  78. void
  79. EncodingUtils::LangGroupForEncoding(const nsACString& aEncoding,
  80. nsACString& aOutGroup)
  81. {
  82. if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue(
  83. encodingsGroups, ArrayLength(encodingsGroups), aEncoding, aOutGroup))) {
  84. aOutGroup.AssignLiteral("x-unicode");
  85. }
  86. }
  87. } // namespace dom
  88. } // namespace mozilla