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/. */
8 * This code was copied from xpcom/ds/nsTextFormatter r1.3
9 * Memory model and Frozen linkage changes only.
10 * -- Prasad <prasad@medhas.org>
13 #ifndef nsTextFormatter_h___
14 #define nsTextFormatter_h___
17 ** API for PR printf like routines. Supports the following formats
19 ** %u - unsigned decimal
21 ** %X - unsigned uppercase hex
22 ** %o - unsigned octal
23 ** %hd, %hu, %hx, %hX, %ho - 16-bit versions of above
24 ** %ld, %lu, %lx, %lX, %lo - 32-bit versions of above
25 ** %lld, %llu, %llx, %llX, %llo - 64 bit versions of above
27 ** %S - char16_t string
29 ** %p - pointer (deals with machine dependent pointer size)
37 #include "mozilla/Span.h"
41 "nsTextFormatter is not available in the standalone glue due to NSPR dependencies."
44 class nsTextFormatter
{
47 * sprintf into a fixed size buffer. Guarantees that the buffer is null
48 * terminated. Returns the length of the written output, NOT including the
49 * null terminator, or (uint32_t)-1 if an error occurs.
51 template <typename
... T
>
52 static uint32_t snprintf(char16_t
* aOut
, uint32_t aOutLen
,
53 const char16_t
* aFmt
, T
... aArgs
) {
54 BoxedValue values
[] = {BoxedValue(aArgs
)...};
55 return vsnprintf(aOut
, aOutLen
, aFmt
,
56 mozilla::Span(values
, sizeof...(aArgs
)));
60 * sprintf into an existing nsAString, overwriting any contents it already
63 template <typename
... T
>
64 static void ssprintf(nsAString
& aOut
, const char16_t
* aFmt
, T
... aArgs
) {
65 BoxedValue values
[] = {BoxedValue(aArgs
)...};
66 vssprintf(aOut
, aFmt
, mozilla::Span(values
, sizeof...(aArgs
)));
86 char16_t
const* mString16
;
94 explicit BoxedValue(int aArg
) : mKind(INT
) { mValue
.mInt
= aArg
; }
96 explicit BoxedValue(unsigned int aArg
) : mKind(UINT
) {
100 explicit BoxedValue(long aArg
) : mKind(INT
) { mValue
.mInt
= aArg
; }
102 explicit BoxedValue(unsigned long aArg
) : mKind(UINT
) {
106 explicit BoxedValue(long long aArg
) : mKind(INT
) { mValue
.mInt
= aArg
; }
108 explicit BoxedValue(unsigned long long aArg
) : mKind(UINT
) {
112 explicit BoxedValue(const void* aArg
) : mKind(POINTER
) {
116 explicit BoxedValue(double aArg
) : mKind(DOUBLE
) { mValue
.mDouble
= aArg
; }
118 explicit BoxedValue(const char* aArg
) : mKind(STRING
) {
119 mValue
.mString
= aArg
;
122 explicit BoxedValue(const char16_t
* aArg
) : mKind(STRING16
) {
123 mValue
.mString16
= aArg
;
126 #if defined(MOZ_USE_CHAR16_WRAPPER)
127 explicit BoxedValue(const char16ptr_t aArg
) : mKind(STRING16
) {
128 mValue
.mString16
= aArg
;
133 explicit BoxedValue(int* aArg
) : mKind(INTPOINTER
) {
134 mValue
.mIntPtr
= aArg
;
137 bool IntCompatible() const { return mKind
== INT
|| mKind
== UINT
; }
139 bool PointerCompatible() const {
140 return mKind
== POINTER
|| mKind
== STRING
|| mKind
== STRING16
||
145 struct SprintfStateStr
;
147 static int fill2(SprintfStateStr
* aState
, const char16_t
* aSrc
, int aSrcLen
,
148 int aWidth
, int aFlags
);
149 static int fill_n(SprintfStateStr
* aState
, const char16_t
* aSrc
, int aSrcLen
,
150 int aWidth
, int aPrec
, int aFlags
);
151 static int cvt_ll(SprintfStateStr
* aState
, uint64_t aNum
, int aWidth
,
152 int aPrec
, int aRadix
, int aFlags
, const char16_t
* aHexStr
);
153 static int cvt_f(SprintfStateStr
* aState
, double aDouble
, int aWidth
,
154 int aPrec
, const char16_t aType
, int aFlags
);
155 static int cvt_S(SprintfStateStr
* aState
, const char16_t
* aStr
, int aWidth
,
156 int aPrec
, int aFlags
);
157 static int cvt_s(SprintfStateStr
* aState
, const char* aStr
, int aWidth
,
158 int aPrec
, int aFlags
);
159 static int dosprintf(SprintfStateStr
* aState
, const char16_t
* aFmt
,
160 mozilla::Span
<BoxedValue
> aValues
);
161 static int StringStuff(SprintfStateStr
* aState
, const char16_t
* aStr
,
163 static int LimitStuff(SprintfStateStr
* aState
, const char16_t
* aStr
,
165 static uint32_t vsnprintf(char16_t
* aOut
, uint32_t aOutLen
,
166 const char16_t
* aFmt
,
167 mozilla::Span
<BoxedValue
> aValues
);
168 static void vssprintf(nsAString
& aOut
, const char16_t
* aFmt
,
169 mozilla::Span
<BoxedValue
> aValues
);
172 #endif /* nsTextFormatter_h___ */