|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsLinebreakConverter_h_ |
|
7 #define nsLinebreakConverter_h_ |
|
8 |
|
9 #include "nscore.h" |
|
10 #include "nsString.h" |
|
11 |
|
12 // utility class for converting between different line breaks. |
|
13 |
|
14 class nsLinebreakConverter |
|
15 { |
|
16 public: |
|
17 |
|
18 // Note: enum must match char* array in GetLinebreakString |
|
19 typedef enum { |
|
20 eLinebreakAny, // any kind of linebreak (i.e. "don't care" source) |
|
21 |
|
22 eLinebreakPlatform, // platform linebreak |
|
23 eLinebreakContent, // Content model linebreak (LF) |
|
24 eLinebreakNet, // Form submission linebreak (CRLF) |
|
25 |
|
26 eLinebreakMac, // CR |
|
27 eLinebreakUnix, // LF |
|
28 eLinebreakWindows, // CRLF |
|
29 |
|
30 eLinebreakSpace // space characters. Only valid as destination type |
|
31 |
|
32 } ELinebreakType; |
|
33 |
|
34 enum { |
|
35 kIgnoreLen = -1 |
|
36 }; |
|
37 |
|
38 /* ConvertLineBreaks |
|
39 * Convert line breaks in the supplied string, allocating and returning |
|
40 * a new buffer. Returns nullptr on failure. |
|
41 * @param aSrc: the source string. if aSrcLen == kIgnoreLen this string is assumed |
|
42 * to be null terminated, otherwise it must be at least aSrcLen long. |
|
43 * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. |
|
44 * If known, pass the known value, as this may be more efficient. |
|
45 * @param aDestBreaks: the line breaks you want in the output. |
|
46 * @param aSrcLen: length of the source. If -1, the source is assumed to be a null- |
|
47 * terminated string. |
|
48 * @param aOutLen: used to return character length of returned buffer, if not null. |
|
49 */ |
|
50 static char* ConvertLineBreaks(const char* aSrc, |
|
51 ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, |
|
52 int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); |
|
53 |
|
54 |
|
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 assumed |
|
59 * to be null terminated, otherwise it must be at least aSrcLen long. |
|
60 * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. |
|
61 * If known, pass the known value, as this may be more efficient. |
|
62 * @param aDestBreaks: the line breaks you want in the output. |
|
63 * @param aSrcLen: length of the source, in characters. If -1, the source is assumed to be a null- |
|
64 * terminated string. |
|
65 * @param aOutLen: used to return character length of returned buffer, if not null. |
|
66 */ |
|
67 static char16_t* ConvertUnicharLineBreaks(const char16_t* aSrc, |
|
68 ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, |
|
69 int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); |
|
70 |
|
71 |
|
72 /* ConvertStringLineBreaks |
|
73 * Convert line breaks in the supplied string, changing the string buffer (i.e. in-place conversion) |
|
74 * @param ioString: the string to be converted. |
|
75 * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. |
|
76 * If known, pass the known value, as this may be more efficient. |
|
77 * @param aDestBreaks: the line breaks you want in the output. |
|
78 * @param aSrcLen: length of the source, in characters. If -1, the source is assumed to be a null- |
|
79 * terminated string. |
|
80 */ |
|
81 static nsresult ConvertStringLineBreaks(nsString& ioString, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks); |
|
82 |
|
83 |
|
84 /* ConvertLineBreaksInSitu |
|
85 * Convert line breaks in place if possible. NOTE: THIS MAY REALLOCATE THE BUFFER, |
|
86 * BUT IT WON'T FREE THE OLD BUFFER (because it doesn't know how). So be prepared |
|
87 * to keep a copy of the old pointer, and free it if this passes back a new pointer. |
|
88 * ALSO NOTE: DON'T PASS A STATIC STRING POINTER TO THIS FUNCTION. |
|
89 * |
|
90 * @param ioBuffer: the source buffer. if aSrcLen == kIgnoreLen this string is assumed |
|
91 * to be null terminated, otherwise it must be at least aSrcLen long. |
|
92 * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. |
|
93 * If known, pass the known value, as this may be more efficient. |
|
94 * @param aDestBreaks: the line breaks you want in the output. |
|
95 * @param aSrcLen: length of the source. If -1, the source is assumed to be a null- |
|
96 * terminated string. |
|
97 * @param aOutLen: used to return character length of returned buffer, if not null. |
|
98 */ |
|
99 static nsresult ConvertLineBreaksInSitu(char **ioBuffer, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, |
|
100 int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); |
|
101 |
|
102 |
|
103 /* ConvertUnicharLineBreaksInSitu |
|
104 * Convert line breaks in place if possible. NOTE: THIS MAY REALLOCATE THE BUFFER, |
|
105 * BUT IT WON'T FREE THE OLD BUFFER (because it doesn't know how). So be prepared |
|
106 * to keep a copy of the old pointer, and free it if this passes back a new pointer. |
|
107 * |
|
108 * @param ioBuffer: the source buffer. if aSrcLen == kIgnoreLen this string is assumed |
|
109 * to be null terminated, otherwise it must be at least aSrcLen long. |
|
110 * @param aSrcBreaks: the line breaks in the source. If unknown, pass eLinebreakAny. |
|
111 * If known, pass the known value, as this may be more efficient. |
|
112 * @param aDestBreaks: the line breaks you want in the output. |
|
113 * @param aSrcLen: length of the source in characters. If -1, the source is assumed to be a null- |
|
114 * terminated string. |
|
115 * @param aOutLen: used to return character length of returned buffer, if not null. |
|
116 */ |
|
117 static nsresult ConvertUnicharLineBreaksInSitu(char16_t **ioBuffer, ELinebreakType aSrcBreaks, ELinebreakType aDestBreaks, |
|
118 int32_t aSrcLen = kIgnoreLen, int32_t* aOutLen = nullptr); |
|
119 |
|
120 }; |
|
121 |
|
122 #endif // nsLinebreakConverter_h_ |