Bug 692145 Separate CountNewlinesIn() to CountNewlinesInXPLength() and CountNewlinesInNativeLength() r=ehsan

This commit is contained in:
Masayuki Nakano 2012-01-31 11:37:26 +09:00
Родитель 960510224f
Коммит da0395c725
1 изменённых файлов: 28 добавлений и 5 удалений

Просмотреть файл

@ -207,16 +207,17 @@ static void AppendSubString(nsAString& aString, nsIContent* aContent,
}
#if defined(XP_WIN)
static PRUint32 CountNewlinesIn(nsIContent* aContent, PRUint32 aMaxOffset)
static PRUint32 CountNewlinesInXPLength(nsIContent* aContent,
PRUint32 aXPLength)
{
NS_ASSERTION(aContent->IsNodeOfType(nsINode::eTEXT),
"aContent is not a text node!");
const nsTextFragment* text = aContent->GetText();
if (!text)
return 0;
NS_ASSERTION(aMaxOffset == PR_UINT32_MAX || aMaxOffset <= text->GetLength(),
NS_ASSERTION(aXPLength == PR_UINT32_MAX || aXPLength <= text->GetLength(),
"text offset is out-of-bounds");
const PRUint32 length = NS_MIN(aMaxOffset, text->GetLength());
const PRUint32 length = NS_MIN(aXPLength, text->GetLength());
PRUint32 newlines = 0;
for (PRUint32 i = 0; i < length; ++i) {
if (text->CharAt(i) == '\n') {
@ -225,6 +226,28 @@ static PRUint32 CountNewlinesIn(nsIContent* aContent, PRUint32 aMaxOffset)
}
return newlines;
}
static PRUint32 CountNewlinesInNativeLength(nsIContent* aContent,
PRUint32 aNativeLength)
{
NS_ASSERTION(aContent->IsNodeOfType(nsINode::eTEXT),
"aContent is not a text node!");
const nsTextFragment* text = aContent->GetText();
if (!text) {
return 0;
}
const PRUint32 xpLength = text->GetLength();
PRUint32 newlines = 0;
for (PRUint32 i = 0, nativeOffset = 0;
i < xpLength && nativeOffset < aNativeLength;
++i, ++nativeOffset) {
if (text->CharAt(i) == '\n') {
++newlines;
++nativeOffset;
}
}
return newlines;
}
#endif
static PRUint32 GetNativeTextLength(nsIContent* aContent, PRUint32 aMaxLength = PR_UINT32_MAX)
@ -239,7 +262,7 @@ static PRUint32 GetNativeTextLength(nsIContent* aContent, PRUint32 aMaxLength =
// On Windows, the length of a native newline ("\r\n") is twice the length of
// the XP newline ("\n"), so XP length is equal to the length of the native
// offset plus the number of newlines encountered in the string.
CountNewlinesIn(aContent, aMaxLength);
CountNewlinesInXPLength(aContent, aMaxLength);
#else
// On other platforms, the native and XP newlines are the same.
0;
@ -271,7 +294,7 @@ static PRUint32 ConvertToXPOffset(nsIContent* aContent, PRUint32 aNativeOffset)
// On Windows, the length of a native newline ("\r\n") is twice the length of
// the XP newline ("\n"), so XP offset is equal to the length of the native
// offset minus the number of newlines encountered in the string.
return aNativeOffset - CountNewlinesIn(aContent, aNativeOffset);
return aNativeOffset - CountNewlinesInNativeLength(aContent, aNativeOffset);
#else
// On other platforms, the native and XP newlines are the same.
return aNativeOffset;