1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsLinebreakConverter_h_
8 #define nsLinebreakConverter_h_
13 // utility class for converting between different line breaks.
15 class nsLinebreakConverter
{
17 // Note: enum must match char* array in GetLinebreakString
19 eLinebreakAny
, // any kind of linebreak (i.e. "don't care" source)
21 eLinebreakPlatform
, // platform linebreak
22 eLinebreakContent
, // Content model linebreak (LF)
23 eLinebreakNet
, // Form submission linebreak (CRLF)
27 eLinebreakWindows
, // CRLF
29 eLinebreakSpace
// space characters. Only valid as destination type
33 enum { kIgnoreLen
= -1 };
36 * Convert line breaks in the supplied string, allocating and returning
37 * a new buffer. Returns nullptr on failure.
38 * @param aSrc: the source string. if aSrcLen == kIgnoreLen this string is
39 * assumed to be null terminated, otherwise it must be at least
41 * @param aSrcBreaks: the line breaks in the source. If unknown, pass
42 * eLinebreakAny. If known, pass the known value, as this may
44 * @param aDestBreaks: the line breaks you want in the output.
45 * @param aSrcLen: length of the source. If -1, the source is assumed to be a
46 * null-terminated string.
47 * @param aOutLen: used to return character length of returned buffer, if not
50 static char* ConvertLineBreaks(const char* aSrc
, ELinebreakType aSrcBreaks
,
51 ELinebreakType aDestBreaks
,
52 int32_t aSrcLen
= kIgnoreLen
,
53 int32_t* aOutLen
= nullptr);
55 /* ConvertUnicharLineBreaks
56 * Convert line breaks in the supplied string, allocating and returning
57 * a new buffer. Returns nullptr on failure.
58 * @param aSrc: the source string. if aSrcLen == kIgnoreLen this string is
59 * assumed to be null terminated, otherwise it must be at least
61 * @param aSrcBreaks: the line breaks in the source. If unknown, pass
62 * eLinebreakAny. If known, pass the known value, as this may
64 * @param aDestBreaks: the line breaks you want in the output.
65 * @param aSrcLen: length of the source, in characters. If -1, the source is
66 * assumed to be a null-terminated string.
67 * @param aOutLen: used to return character length of returned buffer, if not
70 static char16_t
* ConvertUnicharLineBreaks(const char16_t
* aSrc
,
71 ELinebreakType aSrcBreaks
,
72 ELinebreakType aDestBreaks
,
73 int32_t aSrcLen
= kIgnoreLen
,
74 int32_t* aOutLen
= nullptr);
76 /* ConvertStringLineBreaks
77 * Convert line breaks in the supplied string, changing the string buffer
78 * (i.e. in-place conversion)
79 * @param ioString: the string to be converted.
80 * @param aSrcBreaks: the line breaks in the source. If unknown, pass
81 * eLinebreakAny. If known, pass the known value, as this may
83 * @param aDestBreaks: the line breaks you want in the output.
84 * @param aSrcLen: length of the source, in characters. If -1, the source is
85 * assumed to be a null-terminated string.
87 static nsresult
ConvertStringLineBreaks(nsString
& aIoString
,
88 ELinebreakType aSrcBreaks
,
89 ELinebreakType aDestBreaks
);
91 /* ConvertLineBreaksInSitu
92 * Convert line breaks in place if possible. NOTE: THIS MAY REALLOCATE THE
93 * BUFFER, BUT IT WON'T FREE THE OLD BUFFER (because it doesn't know how). So
94 * be prepared to keep a copy of the old pointer, and free it if this passes
95 * back a new pointer. ALSO NOTE: DON'T PASS A STATIC STRING POINTER TO THIS
98 * @param ioBuffer: the source buffer. if aSrcLen == kIgnoreLen this string
99 * is assumed to be null terminated, otherwise it must be at
100 * least aSrcLen long.
101 * @param aSrcBreaks: the line breaks in the source. If unknown, pass
102 * eLinebreakAny. If known, pass the known value, as this may
104 * @param aDestBreaks: the line breaks you want in the output.
105 * @param aSrcLen: length of the source. If -1, the source is assumed to be a
106 * null-terminated string.
107 * @param aOutLen: used to return character length of returned buffer, if not
110 static nsresult
ConvertLineBreaksInSitu(char** aIoBuffer
,
111 ELinebreakType aSrcBreaks
,
112 ELinebreakType aDestBreaks
,
113 int32_t aSrcLen
= kIgnoreLen
,
114 int32_t* aOutLen
= nullptr);
116 /* ConvertUnicharLineBreaksInSitu
117 * Convert line breaks in place if possible. NOTE: THIS MAY REALLOCATE THE
118 * BUFFER, BUT IT WON'T FREE THE OLD BUFFER (because it doesn't know how). So
119 * be prepared to keep a copy of the old pointer, and free it if this passes
120 * back a new pointer.
122 * @param ioBuffer: the source buffer. if aSrcLen == kIgnoreLen this string
123 * is assumed to be null terminated, otherwise it must be at
124 * least aSrcLen long.
125 * @param aSrcBreaks: the line breaks in the source. If unknown, pass
126 * eLinebreakAny. If known, pass the known value, as this may
128 * @param aDestBreaks: the line breaks you want in the output.
129 * @param aSrcLen: length of the source in characters. If -1, the source is
130 * assumed to be a null-terminated string.
131 * @param aOutLen: used to return character length of returned buffer, if not
134 static nsresult
ConvertUnicharLineBreaksInSitu(char16_t
** aIoBuffer
,
135 ELinebreakType aSrcBreaks
,
136 ELinebreakType aDestBreaks
,
137 int32_t aSrcLen
= kIgnoreLen
,
138 int32_t* aOutLen
= nullptr);
141 #endif // nsLinebreakConverter_h_