DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (409f3966645a)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/intl/MozLocale.h"

#include "nsReadableUtils.h"
#include "nsUnicharUtils.h"

#include "unicode/uloc.h"

using namespace mozilla::intl;

/**
 * Note: The file name is `MozLocale` to avoid compilation problems on case-insensitive
 * Windows. The class name is `Locale`.
 */
Locale::Locale(const nsACString& aLocale)
{
  MOZ_ASSERT(!aLocale.IsEmpty(), "Locale string cannot be empty");

  int32_t position = 0;

  if (!IsASCII(aLocale)) {
    mIsWellFormed = false;
    return;
  }

  nsAutoCString normLocale(aLocale);
  normLocale.ReplaceChar('_', '-');

  /**
   * BCP47 language tag:
   *
   * langtag = language            2*3ALPHA
   *           ["-" extlang]       3ALPHA *2("-" 3ALPHA)
   *           ["-" script]        4ALPHA
   *           ["-" region]        2ALPHA / 3DIGIT
   *           *("-" variant)      5*8alphanum / (DIGIT 3alphanum)
   *           *("-" extension)    [0-9a-wy-z] 1*("-" (1*8alphanum))
   *           ["-" privateuse]    x 1*("-" (1*8alphanum))
   *
   * This class currently supports a subset of the full BCP47 language tag
   * with a single extension of allowing variants to be 3ALPHA to support
   * `ja-JP-mac` code:
   *
   * langtag = language            2*3ALPHA
   *           ["-" script]        4ALPHA
   *           ["-" region]        2ALPHA
   *           *("-" variant)      3*8alphanum
   *           ["-"] privateuse]   "x" 1*("-" (1*8alphanum))
   *
   * The `position` variable represents the currently expected section of the tag
   * and intentionally skips positions (like `extlang`) which may be added later.
   *
   *  language-extlangs-script-region-variant-extension-privateuse
   *  --- 0 -- --- 1 -- -- 2 - -- 3 - -- 4 -- --- x --- ---- 6 ---
   */
  for (const nsACString& subTag : normLocale.Split('-')) {
    auto slen = subTag.Length();
    if (slen > 8) {
      mIsWellFormed = false;
      return;
    } else if (position == 6) {
      ToLowerCase(*mPrivateUse.AppendElement(subTag));
    } else if (subTag.LowerCaseEqualsLiteral("x")) {
      position = 6;
    } else if (position == 0) {
      if (slen < 2 || slen > 3) {
        mIsWellFormed = false;
        return;
      }
      mLanguage = subTag;
      ToLowerCase(mLanguage);
      position = 2;
    } else if (position <= 2 && slen == 4) {
      mScript = subTag;
      ToLowerCase(mScript);
      mScript.Replace(0, 1, ToUpperCase(mScript[0]));
      position = 3;
    } else if (position <= 3 && slen == 2) {
      mRegion = subTag;
      ToUpperCase(mRegion);
      position = 4;
    } else if (position <= 4 && slen >= 5 && slen <= 8) {
      nsAutoCString lcSubTag(subTag);
      ToLowerCase(lcSubTag);
      mVariants.InsertElementSorted(lcSubTag);
      position = 4;
    }
  }
}

const nsCString
Locale::AsString() const
{
  nsCString tag;

  if (!mIsWellFormed) {
    tag.AppendLiteral("und");
    return tag;
  }

  tag.Append(mLanguage);

  if (!mScript.IsEmpty()) {
    tag.AppendLiteral("-");
    tag.Append(mScript);
  }

  if (!mRegion.IsEmpty()) {
    tag.AppendLiteral("-");
    tag.Append(mRegion);
  }

  for (const auto& variant : mVariants) {
    tag.AppendLiteral("-");
    tag.Append(variant);
  }

  if (!mPrivateUse.IsEmpty()) {
    if (tag.IsEmpty()) {
      tag.AppendLiteral("x");
    } else {
      tag.AppendLiteral("-x");
    }

    for (const auto& subTag : mPrivateUse) {
      tag.AppendLiteral("-");
      tag.Append(subTag);
    }
  }
  return tag;
}

const nsACString&
Locale::GetLanguage() const
{
  return mLanguage;
}

const nsACString&
Locale::GetScript() const
{
  return mScript;
}

const nsACString&
Locale::GetRegion() const
{
  return mRegion;
}

const nsTArray<nsCString>&
Locale::GetVariants() const
{
  return mVariants;
}

bool
Locale::Matches(const Locale& aOther, bool aThisRange, bool aOtherRange) const
{
  if (!IsWellFormed() || !aOther.IsWellFormed()) {
    return false;
  }

  if ((!aThisRange || !mLanguage.IsEmpty()) &&
      (!aOtherRange || !aOther.mLanguage.IsEmpty()) &&
      !mLanguage.Equals(aOther.mLanguage)) {
    return false;
  }

  if ((!aThisRange || !mScript.IsEmpty()) &&
      (!aOtherRange || !aOther.mScript.IsEmpty()) &&
      !mScript.Equals(aOther.mScript)) {
    return false;
  }
  if ((!aThisRange || !mRegion.IsEmpty()) &&
      (!aOtherRange || !aOther.mRegion.IsEmpty()) &&
      !mRegion.Equals(aOther.mRegion)) {
    return false;
  }
  if ((!aThisRange || !mVariants.IsEmpty()) &&
      (!aOtherRange || !aOther.mVariants.IsEmpty()) &&
      mVariants != aOther.mVariants) {
    return false;
  }
  return true;
}

bool
Locale::AddLikelySubtags()
{
  const int32_t kLocaleMax = 160;
  char maxLocale[kLocaleMax];

  UErrorCode status = U_ZERO_ERROR;
  uloc_addLikelySubtags(AsString().get(), maxLocale, kLocaleMax, &status);

  if (U_FAILURE(status)) {
    return false;
  }

  nsDependentCString maxLocStr(maxLocale);
  Locale loc = Locale(maxLocStr);

  if (loc == *this) {
    return false;
  }

  mLanguage = loc.mLanguage;
  mScript = loc.mScript;
  mRegion = loc.mRegion;

  // We don't update variant from likelySubtag since it's not going to
  // provide it and we want to preserve the range

  return true;
}

void
Locale::ClearVariants()
{
  mVariants.Clear();
}

void
Locale::ClearRegion()
{
  mRegion.Truncate();
}