1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/io/nsLinebreakConverter.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsLinebreakConverter_h_ 1.10 +#define nsLinebreakConverter_h_ 1.11 + 1.12 +#include "nscore.h" 1.13 +#include "nsString.h" 1.14 + 1.15 +// utility class for converting between different line breaks. 1.16 + 1.17 +class nsLinebreakConverter 1.18 +{ 1.19 +public: 1.20 + 1.21 + // Note: enum must match char* array in GetLinebreakString 1.22 + typedef enum { 1.23 + eLinebreakAny, // any kind of linebreak (i.e. "don't care" source) 1.24 + 1.25 + eLinebreakPlatform, // platform linebreak 1.26 + eLinebreakContent, // Content model linebreak (LF) 1.27 + eLinebreakNet, // Form submission linebreak (CRLF) 1.28 + 1.29 + eLinebreakMac, // CR 1.30 + eLinebreakUnix, // LF 1.31 + eLinebreakWindows, // CRLF 1.32 + 1.33 + eLinebreakSpace // space characters. Only valid as destination type 1.34 + 1.35 + } ELinebreakType; 1.36 + 1.37 + enum { 1.38 + kIgnoreLen = -1 1.39 + }; 1.40 + 1.41 + /* ConvertLineBreaks 1.42 + * Convert line breaks in the supplied string, allocating and returning 1.43 + * a new buffer. Returns nullptr on failure. 1.44 + * @param aSrc: the source string. if aSrcLen == kIgnoreLen this string is assumed 1.45 + * to be null terminated, otherwise it must be at least aSrcLen long. 1.46 + * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. 1.47 + * If known, pass the known value, as this may be more efficient. 1.48 + * @param aDestBreaks: the line breaks you want in the output. 1.49 + * @param aSrcLen: length of the source. If -1, the source is assumed to be a null- 1.50 + * terminated string. 1.51 + * @param aOutLen: used to return character length of returned buffer, if not null. 1.52 + */ 1.53 + static char* ConvertLineBreaks(const char* aSrc, 1.54 + ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, 1.55 + int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); 1.56 + 1.57 + 1.58 + /* ConvertUnicharLineBreaks 1.59 + * Convert line breaks in the supplied string, allocating and returning 1.60 + * a new buffer. Returns nullptr on failure. 1.61 + * @param aSrc: the source string. if aSrcLen == kIgnoreLen this string is assumed 1.62 + * to be null terminated, otherwise it must be at least aSrcLen long. 1.63 + * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. 1.64 + * If known, pass the known value, as this may be more efficient. 1.65 + * @param aDestBreaks: the line breaks you want in the output. 1.66 + * @param aSrcLen: length of the source, in characters. If -1, the source is assumed to be a null- 1.67 + * terminated string. 1.68 + * @param aOutLen: used to return character length of returned buffer, if not null. 1.69 + */ 1.70 + static char16_t* ConvertUnicharLineBreaks(const char16_t* aSrc, 1.71 + ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, 1.72 + int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); 1.73 + 1.74 + 1.75 + /* ConvertStringLineBreaks 1.76 + * Convert line breaks in the supplied string, changing the string buffer (i.e. in-place conversion) 1.77 + * @param ioString: the string to be converted. 1.78 + * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. 1.79 + * If known, pass the known value, as this may be more efficient. 1.80 + * @param aDestBreaks: the line breaks you want in the output. 1.81 + * @param aSrcLen: length of the source, in characters. If -1, the source is assumed to be a null- 1.82 + * terminated string. 1.83 + */ 1.84 + static nsresult ConvertStringLineBreaks(nsString& ioString, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks); 1.85 + 1.86 + 1.87 + /* ConvertLineBreaksInSitu 1.88 + * Convert line breaks in place if possible. NOTE: THIS MAY REALLOCATE THE BUFFER, 1.89 + * BUT IT WON'T FREE THE OLD BUFFER (because it doesn't know how). So be prepared 1.90 + * to keep a copy of the old pointer, and free it if this passes back a new pointer. 1.91 + * ALSO NOTE: DON'T PASS A STATIC STRING POINTER TO THIS FUNCTION. 1.92 + * 1.93 + * @param ioBuffer: the source buffer. if aSrcLen == kIgnoreLen this string is assumed 1.94 + * to be null terminated, otherwise it must be at least aSrcLen long. 1.95 + * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. 1.96 + * If known, pass the known value, as this may be more efficient. 1.97 + * @param aDestBreaks: the line breaks you want in the output. 1.98 + * @param aSrcLen: length of the source. If -1, the source is assumed to be a null- 1.99 + * terminated string. 1.100 + * @param aOutLen: used to return character length of returned buffer, if not null. 1.101 + */ 1.102 + static nsresult ConvertLineBreaksInSitu(char **ioBuffer, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, 1.103 + int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); 1.104 + 1.105 + 1.106 + /* ConvertUnicharLineBreaksInSitu 1.107 + * Convert line breaks in place if possible. NOTE: THIS MAY REALLOCATE THE BUFFER, 1.108 + * BUT IT WON'T FREE THE OLD BUFFER (because it doesn't know how). So be prepared 1.109 + * to keep a copy of the old pointer, and free it if this passes back a new pointer. 1.110 + * 1.111 + * @param ioBuffer: the source buffer. if aSrcLen == kIgnoreLen this string is assumed 1.112 + * to be null terminated, otherwise it must be at least aSrcLen long. 1.113 + * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. 1.114 + * If known, pass the known value, as this may be more efficient. 1.115 + * @param aDestBreaks: the line breaks you want in the output. 1.116 + * @param aSrcLen: length of the source in characters. If -1, the source is assumed to be a null- 1.117 + * terminated string. 1.118 + * @param aOutLen: used to return character length of returned buffer, if not null. 1.119 + */ 1.120 + static nsresult ConvertUnicharLineBreaksInSitu(char16_t **ioBuffer, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, 1.121 + int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); 1.122 + 1.123 +}; 1.124 + 1.125 +#endif // nsLinebreakConverter_h_