2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
1999-12-02 01:21:30 +03:00
|
|
|
|
|
|
|
#include "nsLinebreakConverter.h"
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
#include "nsMemory.h"
|
1999-12-02 01:21:30 +03:00
|
|
|
#include "nsCRT.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
2015-04-09 20:25:05 +03:00
|
|
|
GetLinebreakString
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-04-09 20:25:05 +03:00
|
|
|
Could make this inline
|
1999-12-02 01:21:30 +03:00
|
|
|
----------------------------------------------------------------------------*/
|
2014-05-15 00:15:46 +04:00
|
|
|
static const char*
|
|
|
|
GetLinebreakString(nsLinebreakConverter::ELinebreakType aBreakType)
|
1999-12-02 01:21:30 +03:00
|
|
|
{
|
2001-11-07 09:24:10 +03:00
|
|
|
static const char* const sLinebreaks[] = {
|
1999-12-02 01:21:30 +03:00
|
|
|
"", // any
|
|
|
|
NS_LINEBREAK, // platform
|
|
|
|
LFSTR, // content
|
|
|
|
CRLF, // net
|
|
|
|
CRSTR, // Mac
|
|
|
|
LFSTR, // Unix
|
|
|
|
CRLF, // Windows
|
2010-11-23 11:50:55 +03:00
|
|
|
" ", // space
|
2014-05-05 21:30:43 +04:00
|
|
|
nullptr
|
1999-12-02 01:21:30 +03:00
|
|
|
};
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
return sLinebreaks[aBreakType];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
2015-04-09 20:25:05 +03:00
|
|
|
AppendLinebreak
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-04-09 20:25:05 +03:00
|
|
|
Wee inline method to append a line break. Modifies ioDest.
|
1999-12-02 01:21:30 +03:00
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
template<class T>
|
2014-05-15 00:15:46 +04:00
|
|
|
void
|
|
|
|
AppendLinebreak(T*& aIoDest, const char* aLineBreakStr)
|
1999-12-02 01:21:30 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
*aIoDest++ = *aLineBreakStr;
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aLineBreakStr[1]) {
|
|
|
|
*aIoDest++ = aLineBreakStr[1];
|
|
|
|
}
|
1999-12-02 01:21:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
2015-04-09 20:25:05 +03:00
|
|
|
CountChars
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-04-09 20:25:05 +03:00
|
|
|
Counts occurrences of breakStr in aSrc
|
1999-12-02 01:21:30 +03:00
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
template<class T>
|
2014-05-15 00:15:46 +04:00
|
|
|
int32_t
|
|
|
|
CountLinebreaks(const T* aSrc, int32_t aInLen, const char* aBreakStr)
|
1999-12-02 01:21:30 +03:00
|
|
|
{
|
|
|
|
const T* src = aSrc;
|
2014-05-15 00:15:46 +04:00
|
|
|
const T* srcEnd = aSrc + aInLen;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t theCount = 0;
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
while (src < srcEnd) {
|
|
|
|
if (*src == *aBreakStr) {
|
2005-09-17 01:06:21 +04:00
|
|
|
src++;
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aBreakStr[1]) {
|
|
|
|
if (src < srcEnd && *src == aBreakStr[1]) {
|
2005-09-17 01:06:21 +04:00
|
|
|
src++;
|
|
|
|
theCount++;
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2005-09-17 01:06:21 +04:00
|
|
|
theCount++;
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2005-09-17 01:06:21 +04:00
|
|
|
src++;
|
1999-12-02 01:21:30 +03:00
|
|
|
}
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
return theCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
2015-04-09 20:25:05 +03:00
|
|
|
ConvertBreaks
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-04-09 20:25:05 +03:00
|
|
|
ioLen *includes* a terminating null, if any
|
1999-12-02 01:21:30 +03:00
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
template<class T>
|
2014-05-15 00:15:46 +04:00
|
|
|
static T*
|
|
|
|
ConvertBreaks(const T* aInSrc, int32_t& aIoLen, const char* aSrcBreak,
|
|
|
|
const char* aDestBreak)
|
1999-12-02 01:21:30 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(aInSrc && aSrcBreak && aDestBreak, "Got a null string");
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
T* resultString = nullptr;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
// handle the no conversion case
|
2014-05-15 00:15:46 +04:00
|
|
|
if (nsCRT::strcmp(aSrcBreak, aDestBreak) == 0) {
|
2015-05-04 08:33:00 +03:00
|
|
|
resultString = (T*)malloc(sizeof(T) * aIoLen);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!resultString) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
memcpy(resultString, aInSrc, sizeof(T) * aIoLen); // includes the null, if any
|
1999-12-02 01:21:30 +03:00
|
|
|
return resultString;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
int32_t srcBreakLen = strlen(aSrcBreak);
|
|
|
|
int32_t destBreakLen = strlen(aDestBreak);
|
1999-12-02 01:21:30 +03:00
|
|
|
|
|
|
|
// handle the easy case, where the string length does not change, and the
|
|
|
|
// breaks are only 1 char long, i.e. CR <-> LF
|
2014-05-15 00:15:46 +04:00
|
|
|
if (srcBreakLen == destBreakLen && srcBreakLen == 1) {
|
2015-05-04 08:33:00 +03:00
|
|
|
resultString = (T*)malloc(sizeof(T) * aIoLen);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!resultString) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T* src = aInSrc;
|
|
|
|
const T* srcEnd = aInSrc + aIoLen; // includes null, if any
|
|
|
|
T* dst = resultString;
|
|
|
|
|
|
|
|
char srcBreakChar = *aSrcBreak; // we know it's one char long already
|
|
|
|
char dstBreakChar = *aDestBreak;
|
|
|
|
|
|
|
|
while (src < srcEnd) {
|
|
|
|
if (*src == srcBreakChar) {
|
1999-12-02 01:21:30 +03:00
|
|
|
*dst++ = dstBreakChar;
|
|
|
|
src++;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
1999-12-02 01:21:30 +03:00
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
// aIoLen does not change
|
|
|
|
} else {
|
1999-12-02 01:21:30 +03:00
|
|
|
// src and dest termination is different length. Do it a slower way.
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
// count linebreaks in src. Assumes that chars in 2-char linebreaks are unique.
|
2014-05-15 00:15:46 +04:00
|
|
|
int32_t numLinebreaks = CountLinebreaks(aInSrc, aIoLen, aSrcBreak);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
int32_t newBufLen =
|
|
|
|
aIoLen - (numLinebreaks * srcBreakLen) + (numLinebreaks * destBreakLen);
|
2015-05-04 08:33:00 +03:00
|
|
|
resultString = (T*)malloc(sizeof(T) * newBufLen);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!resultString) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
const T* src = aInSrc;
|
|
|
|
const T* srcEnd = aInSrc + aIoLen; // includes null, if any
|
|
|
|
T* dst = resultString;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
while (src < srcEnd) {
|
|
|
|
if (*src == *aSrcBreak) {
|
|
|
|
*dst++ = *aDestBreak;
|
|
|
|
if (aDestBreak[1]) {
|
|
|
|
*dst++ = aDestBreak[1];
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2005-09-17 01:06:21 +04:00
|
|
|
src++;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (src < srcEnd && aSrcBreak[1] && *src == aSrcBreak[1]) {
|
2005-09-17 01:06:21 +04:00
|
|
|
src++;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
} else {
|
1999-12-02 01:21:30 +03:00
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
aIoLen = newBufLen;
|
1999-12-02 01:21:30 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
return resultString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
2014-05-05 21:30:43 +04:00
|
|
|
ConvertBreaksInSitu
|
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
Convert breaks in situ. Can only do this if the linebreak length
|
|
|
|
does not change.
|
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
template<class T>
|
2014-05-15 00:15:46 +04:00
|
|
|
static void
|
|
|
|
ConvertBreaksInSitu(T* aInSrc, int32_t aInLen, char aSrcBreak, char aDestBreak)
|
1999-12-02 01:21:30 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
T* src = aInSrc;
|
|
|
|
T* srcEnd = aInSrc + aInLen;
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
while (src < srcEnd) {
|
|
|
|
if (*src == aSrcBreak) {
|
|
|
|
*src = aDestBreak;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2005-09-17 01:06:21 +04:00
|
|
|
src++;
|
1999-12-02 01:21:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
2014-05-05 21:30:43 +04:00
|
|
|
ConvertUnknownBreaks
|
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
Convert unknown line breaks to the specified break.
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
This will convert CRLF pairs to one break, and single CR or LF to a break.
|
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
template<class T>
|
2014-05-15 00:15:46 +04:00
|
|
|
static T*
|
|
|
|
ConvertUnknownBreaks(const T* aInSrc, int32_t& aIoLen, const char* aDestBreak)
|
1999-12-02 01:21:30 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
const T* src = aInSrc;
|
|
|
|
const T* srcEnd = aInSrc + aIoLen; // includes null, if any
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
int32_t destBreakLen = strlen(aDestBreak);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t finalLen = 0;
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
while (src < srcEnd) {
|
|
|
|
if (*src == nsCRT::CR) {
|
|
|
|
if (src < srcEnd && src[1] == nsCRT::LF) {
|
1999-12-02 01:21:30 +03:00
|
|
|
// CRLF
|
|
|
|
finalLen += destBreakLen;
|
2005-09-17 01:06:21 +04:00
|
|
|
src++;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
1999-12-02 01:21:30 +03:00
|
|
|
// Lone CR
|
|
|
|
finalLen += destBreakLen;
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (*src == nsCRT::LF) {
|
1999-12-02 01:21:30 +03:00
|
|
|
// Lone LF
|
|
|
|
finalLen += destBreakLen;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2005-09-17 01:06:21 +04:00
|
|
|
finalLen++;
|
1999-12-02 01:21:30 +03:00
|
|
|
}
|
2005-09-17 01:06:21 +04:00
|
|
|
src++;
|
1999-12-02 01:21:30 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-05-04 08:33:00 +03:00
|
|
|
T* resultString = (T*)malloc(sizeof(T) * finalLen);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!resultString) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
src = aInSrc;
|
|
|
|
srcEnd = aInSrc + aIoLen; // includes null, if any
|
1999-12-02 01:21:30 +03:00
|
|
|
|
|
|
|
T* dst = resultString;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
while (src < srcEnd) {
|
|
|
|
if (*src == nsCRT::CR) {
|
|
|
|
if (src < srcEnd && src[1] == nsCRT::LF) {
|
1999-12-02 01:21:30 +03:00
|
|
|
// CRLF
|
2014-05-15 00:15:46 +04:00
|
|
|
AppendLinebreak(dst, aDestBreak);
|
2005-09-17 01:06:21 +04:00
|
|
|
src++;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
1999-12-02 01:21:30 +03:00
|
|
|
// Lone CR
|
2014-05-15 00:15:46 +04:00
|
|
|
AppendLinebreak(dst, aDestBreak);
|
1999-12-02 01:21:30 +03:00
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (*src == nsCRT::LF) {
|
1999-12-02 01:21:30 +03:00
|
|
|
// Lone LF
|
2014-05-15 00:15:46 +04:00
|
|
|
AppendLinebreak(dst, aDestBreak);
|
|
|
|
} else {
|
1999-12-02 01:21:30 +03:00
|
|
|
*dst++ = *src;
|
|
|
|
}
|
2005-09-17 01:06:21 +04:00
|
|
|
src++;
|
1999-12-02 01:21:30 +03:00
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
aIoLen = finalLen;
|
1999-12-02 01:21:30 +03:00
|
|
|
return resultString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
2015-04-09 20:25:05 +03:00
|
|
|
ConvertLineBreaks
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
----------------------------------------------------------------------------*/
|
2014-05-15 00:15:46 +04:00
|
|
|
char*
|
|
|
|
nsLinebreakConverter::ConvertLineBreaks(const char* aSrc,
|
|
|
|
ELinebreakType aSrcBreaks,
|
|
|
|
ELinebreakType aDestBreaks,
|
|
|
|
int32_t aSrcLen, int32_t* aOutLen)
|
1999-12-02 01:21:30 +03:00
|
|
|
{
|
2010-11-23 11:50:55 +03:00
|
|
|
NS_ASSERTION(aDestBreaks != eLinebreakAny &&
|
|
|
|
aSrcBreaks != eLinebreakSpace, "Invalid parameter");
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!aSrc) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t sourceLen = (aSrcLen == kIgnoreLen) ? strlen(aSrc) + 1 : aSrcLen;
|
1999-12-02 01:21:30 +03:00
|
|
|
|
|
|
|
char* resultString;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aSrcBreaks == eLinebreakAny) {
|
2014-08-25 23:17:15 +04:00
|
|
|
resultString = ConvertUnknownBreaks(aSrc, sourceLen,
|
|
|
|
GetLinebreakString(aDestBreaks));
|
2014-05-15 00:15:46 +04:00
|
|
|
} else
|
|
|
|
resultString = ConvertBreaks(aSrc, sourceLen,
|
|
|
|
GetLinebreakString(aSrcBreaks),
|
|
|
|
GetLinebreakString(aDestBreaks));
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aOutLen) {
|
|
|
|
*aOutLen = sourceLen;
|
|
|
|
}
|
1999-12-02 01:21:30 +03:00
|
|
|
return resultString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
2015-04-09 20:25:05 +03:00
|
|
|
ConvertLineBreaksInSitu
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
----------------------------------------------------------------------------*/
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult
|
|
|
|
nsLinebreakConverter::ConvertLineBreaksInSitu(char** aIoBuffer,
|
|
|
|
ELinebreakType aSrcBreaks,
|
|
|
|
ELinebreakType aDestBreaks,
|
|
|
|
int32_t aSrcLen, int32_t* aOutLen)
|
1999-12-02 01:21:30 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(aIoBuffer && *aIoBuffer, "Null pointer passed");
|
|
|
|
if (!aIoBuffer || !*aIoBuffer) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2010-11-23 11:50:55 +03:00
|
|
|
NS_ASSERTION(aDestBreaks != eLinebreakAny &&
|
|
|
|
aSrcBreaks != eLinebreakSpace, "Invalid parameter");
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
int32_t sourceLen = (aSrcLen == kIgnoreLen) ? strlen(*aIoBuffer) + 1 : aSrcLen;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
// can we convert in-place?
|
|
|
|
const char* srcBreaks = GetLinebreakString(aSrcBreaks);
|
|
|
|
const char* dstBreaks = GetLinebreakString(aDestBreaks);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aSrcBreaks != eLinebreakAny &&
|
|
|
|
strlen(srcBreaks) == 1 &&
|
|
|
|
strlen(dstBreaks) == 1) {
|
|
|
|
ConvertBreaksInSitu(*aIoBuffer, sourceLen, *srcBreaks, *dstBreaks);
|
|
|
|
if (aOutLen) {
|
|
|
|
*aOutLen = sourceLen;
|
|
|
|
}
|
|
|
|
} else {
|
1999-12-02 01:21:30 +03:00
|
|
|
char* destBuffer;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aSrcBreaks == eLinebreakAny) {
|
|
|
|
destBuffer = ConvertUnknownBreaks(*aIoBuffer, sourceLen, dstBreaks);
|
|
|
|
} else {
|
|
|
|
destBuffer = ConvertBreaks(*aIoBuffer, sourceLen, srcBreaks, dstBreaks);
|
|
|
|
}
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!destBuffer) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
*aIoBuffer = destBuffer;
|
|
|
|
if (aOutLen) {
|
|
|
|
*aOutLen = sourceLen;
|
|
|
|
}
|
1999-12-02 01:21:30 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
2015-04-09 20:25:05 +03:00
|
|
|
ConvertUnicharLineBreaks
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
----------------------------------------------------------------------------*/
|
2014-05-15 00:15:46 +04:00
|
|
|
char16_t*
|
|
|
|
nsLinebreakConverter::ConvertUnicharLineBreaks(const char16_t* aSrc,
|
|
|
|
ELinebreakType aSrcBreaks,
|
|
|
|
ELinebreakType aDestBreaks,
|
|
|
|
int32_t aSrcLen,
|
|
|
|
int32_t* aOutLen)
|
1999-12-02 01:21:30 +03:00
|
|
|
{
|
2010-11-23 11:50:55 +03:00
|
|
|
NS_ASSERTION(aDestBreaks != eLinebreakAny &&
|
|
|
|
aSrcBreaks != eLinebreakSpace, "Invalid parameter");
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!aSrc) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bufLen = (aSrcLen == kIgnoreLen) ? NS_strlen(aSrc) + 1 : aSrcLen;
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* resultString;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aSrcBreaks == eLinebreakAny) {
|
2014-08-25 23:17:15 +04:00
|
|
|
resultString = ConvertUnknownBreaks(aSrc, bufLen,
|
|
|
|
GetLinebreakString(aDestBreaks));
|
2014-05-15 00:15:46 +04:00
|
|
|
} else
|
|
|
|
resultString = ConvertBreaks(aSrc, bufLen, GetLinebreakString(aSrcBreaks),
|
|
|
|
GetLinebreakString(aDestBreaks));
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aOutLen) {
|
|
|
|
*aOutLen = bufLen;
|
|
|
|
}
|
1999-12-02 01:21:30 +03:00
|
|
|
return resultString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
2015-04-09 20:25:05 +03:00
|
|
|
ConvertStringLineBreaks
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
----------------------------------------------------------------------------*/
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult
|
|
|
|
nsLinebreakConverter::ConvertUnicharLineBreaksInSitu(
|
|
|
|
char16_t** aIoBuffer, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks,
|
|
|
|
int32_t aSrcLen, int32_t* aOutLen)
|
1999-12-02 01:21:30 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(aIoBuffer && *aIoBuffer, "Null pointer passed");
|
|
|
|
if (!aIoBuffer || !*aIoBuffer) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2010-11-23 11:50:55 +03:00
|
|
|
NS_ASSERTION(aDestBreaks != eLinebreakAny &&
|
|
|
|
aSrcBreaks != eLinebreakSpace, "Invalid parameter");
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
int32_t sourceLen =
|
|
|
|
(aSrcLen == kIgnoreLen) ? NS_strlen(*aIoBuffer) + 1 : aSrcLen;
|
1999-12-02 01:21:30 +03:00
|
|
|
|
|
|
|
// can we convert in-place?
|
|
|
|
const char* srcBreaks = GetLinebreakString(aSrcBreaks);
|
|
|
|
const char* dstBreaks = GetLinebreakString(aDestBreaks);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if ((aSrcBreaks != eLinebreakAny) &&
|
|
|
|
(strlen(srcBreaks) == 1) &&
|
|
|
|
(strlen(dstBreaks) == 1)) {
|
|
|
|
ConvertBreaksInSitu(*aIoBuffer, sourceLen, *srcBreaks, *dstBreaks);
|
|
|
|
if (aOutLen) {
|
|
|
|
*aOutLen = sourceLen;
|
|
|
|
}
|
|
|
|
} else {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* destBuffer;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aSrcBreaks == eLinebreakAny) {
|
|
|
|
destBuffer = ConvertUnknownBreaks(*aIoBuffer, sourceLen, dstBreaks);
|
|
|
|
} else {
|
|
|
|
destBuffer = ConvertBreaks(*aIoBuffer, sourceLen, srcBreaks, dstBreaks);
|
|
|
|
}
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!destBuffer) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
*aIoBuffer = destBuffer;
|
|
|
|
if (aOutLen) {
|
|
|
|
*aOutLen = sourceLen;
|
|
|
|
}
|
1999-12-02 01:21:30 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
2015-04-09 20:25:05 +03:00
|
|
|
ConvertStringLineBreaks
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
----------------------------------------------------------------------------*/
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult
|
|
|
|
nsLinebreakConverter::ConvertStringLineBreaks(nsString& aIoString,
|
|
|
|
ELinebreakType aSrcBreaks,
|
|
|
|
ELinebreakType aDestBreaks)
|
1999-12-02 01:21:30 +03:00
|
|
|
{
|
|
|
|
|
2010-11-23 11:50:55 +03:00
|
|
|
NS_ASSERTION(aDestBreaks != eLinebreakAny &&
|
|
|
|
aSrcBreaks != eLinebreakSpace, "Invalid parameter");
|
1999-12-02 01:21:30 +03:00
|
|
|
|
2000-03-02 22:36:15 +03:00
|
|
|
// nothing to do
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aIoString.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
nsresult rv;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2002-01-12 01:40:09 +03:00
|
|
|
// remember the old buffer in case
|
|
|
|
// we blow it away later
|
|
|
|
nsString::char_iterator stringBuf;
|
2016-05-06 01:50:35 +03:00
|
|
|
if (!aIoString.BeginWriting(stringBuf, fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t newLen;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2002-01-12 01:40:09 +03:00
|
|
|
rv = ConvertUnicharLineBreaksInSitu(&stringBuf,
|
|
|
|
aSrcBreaks, aDestBreaks,
|
2014-05-15 00:15:46 +04:00
|
|
|
aIoString.Length() + 1, &newLen);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2002-01-12 01:40:09 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (stringBuf != aIoString.get()) {
|
2016-05-06 01:52:16 +03:00
|
|
|
aIoString.Adopt(stringBuf, newLen - 1);
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1999-12-02 01:21:30 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|