Bug 1040668 part 14 - Add helper function nsStyleUtil::MatchesLanguagePrefix for doing simple language matching. r=dbaron

This commit is contained in:
Xidorn Quan 2015-11-28 11:56:33 +11:00
parent c338260f90
commit 40e248e972
2 changed files with 27 additions and 5 deletions

View File

@ -21,7 +21,6 @@
#include "nsCOMPtr.h"
#include "nsBlockFrame.h"
#include "nsCRT.h"
#include "nsFontMetrics.h"
#include "nsSplittableFrame.h"
#include "nsLineLayout.h"
@ -52,6 +51,7 @@
#include "MathMLTextRunFactory.h"
#include "nsExpirationTracker.h"
#include "nsUnicodeProperties.h"
#include "nsStyleUtil.h"
#include "nsTextFragment.h"
#include "nsGkAtoms.h"
@ -2910,10 +2910,8 @@ static bool IsChineseOrJapanese(nsTextFrame* aFrame)
if (!language) {
return false;
}
const char16_t *lang = language->GetUTF16String();
return (!nsCRT::strncmp(lang, MOZ_UTF16("ja"), 2) ||
!nsCRT::strncmp(lang, MOZ_UTF16("zh"), 2)) &&
(language->GetLength() == 2 || lang[2] == '-');
return nsStyleUtil::MatchesLanguagePrefix(language, MOZ_UTF16("ja")) ||
nsStyleUtil::MatchesLanguagePrefix(language, MOZ_UTF16("zh"));
}
#ifdef DEBUG

View File

@ -11,6 +11,7 @@
#include "nsTArrayForwardDeclare.h"
#include "gfxFontFamilyList.h"
#include "nsStyleStruct.h"
#include "nsCRT.h"
class nsCSSValue;
class nsStringComparator;
@ -175,6 +176,29 @@ public:
const nsSubstring& aStyleText,
nsresult* aRv);
template<size_t N>
static bool MatchesLanguagePrefix(const char16_t* aLang, size_t aLen,
const char16_t (&aPrefix)[N])
{
return !nsCRT::strncmp(aLang, aPrefix, N - 1) &&
(aLen == N - 1 || aLang[N - 1] == '-');
}
template<size_t N>
static bool MatchesLanguagePrefix(const nsIAtom* aLang,
const char16_t (&aPrefix)[N])
{
MOZ_ASSERT(aLang);
return MatchesLanguagePrefix(aLang->GetUTF16String(),
aLang->GetLength(), aPrefix);
}
template<size_t N>
static bool MatchesLanguagePrefix(const nsAString& aLang,
const char16_t (&aPrefix)[N])
{
return MatchesLanguagePrefix(aLang.Data(), aLang.Length(), aPrefix);
}
};