2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
2017-01-13 20:25:59 +03: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/. */
|
|
|
|
|
2017-01-06 01:43:45 +03:00
|
|
|
/* Printf-like functions, with canned variants that malloc their result. */
|
|
|
|
|
2017-01-13 20:25:59 +03:00
|
|
|
#ifndef mozilla_Printf_h
|
|
|
|
#define mozilla_Printf_h
|
|
|
|
|
|
|
|
/*
|
2017-01-06 01:43:45 +03:00
|
|
|
** API for PR printf like routines.
|
|
|
|
**
|
|
|
|
** These exist partly for historical reasons -- initially they were in
|
|
|
|
** NSPR, then forked in tree and modified in js/ -- but now the prime
|
|
|
|
** motivation is both closer control over the exact formatting (with
|
|
|
|
** one exception, see below) and also the ability to control where
|
|
|
|
** exactly the generated results are sent.
|
|
|
|
**
|
|
|
|
** It might seem that this could all be dispensed with in favor of a
|
|
|
|
** wrapper around |vsnprintf| -- except that this implementation
|
|
|
|
** guarantees that the %s format will accept a NULL pointer, whereas
|
|
|
|
** with standard functions this is undefined.
|
|
|
|
**
|
|
|
|
** This supports the following formats. It implements a subset of the
|
|
|
|
** standard formats; due to the use of MOZ_FORMAT_PRINTF, it is not
|
|
|
|
** permissible to extend the standard, aside from relaxing undefined
|
|
|
|
** behavior.
|
|
|
|
**
|
2017-01-13 20:25:59 +03:00
|
|
|
** %d - decimal
|
|
|
|
** %u - unsigned decimal
|
|
|
|
** %x - unsigned hex
|
|
|
|
** %X - unsigned uppercase hex
|
|
|
|
** %o - unsigned octal
|
|
|
|
** %hd, %hu, %hx, %hX, %ho - "short" versions of above
|
|
|
|
** %ld, %lu, %lx, %lX, %lo - "long" versions of above
|
|
|
|
** %lld, %llu, %llx, %llX, %llo - "long long" versions of above
|
|
|
|
** %zd, %zo, %zu, %zx, %zX - size_t versions of above
|
2017-07-26 23:22:58 +03:00
|
|
|
** %Id, %Io, %Iu, %Ix, %IX - size_t versions of above (for Windows compat).
|
|
|
|
** Note that MSVC 2015 and newer supports the z length modifier so
|
|
|
|
** users should prefer using %z instead of %I. We are supporting %I in
|
|
|
|
** addition to %z in case third-party code that uses %I gets routed to
|
|
|
|
** use this printf implementation.
|
2017-01-13 20:25:59 +03:00
|
|
|
** %s - string
|
2017-01-06 01:43:45 +03:00
|
|
|
** %S, %ls - wide string, that is wchar_t*
|
2017-01-13 20:25:59 +03:00
|
|
|
** %c - character
|
|
|
|
** %p - pointer (deals with machine dependent pointer size)
|
2017-01-06 01:43:45 +03:00
|
|
|
** %f - float; note that this is actually formatted using the
|
|
|
|
** system's native printf, and so the results may vary
|
|
|
|
** %g - float; note that this is actually formatted using the
|
|
|
|
** system's native printf, and so the results may vary
|
2017-01-13 20:25:59 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mozilla/AllocPolicy.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
|
|
|
#include "mozilla/Types.h"
|
2017-03-03 18:17:27 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2017-01-13 20:25:59 +03:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This class may be subclassed to provide a way to get the output of
|
|
|
|
* a printf-like call, as the output is generated.
|
|
|
|
*/
|
|
|
|
class PrintfTarget {
|
|
|
|
public:
|
|
|
|
/* The Printf-like interface. */
|
|
|
|
bool MFBT_API print(const char* format, ...) MOZ_FORMAT_PRINTF(2, 3);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-01-13 20:25:59 +03:00
|
|
|
/* The Vprintf-like interface. */
|
2021-02-03 00:18:06 +03:00
|
|
|
bool MFBT_API vprint(const char* fmt, va_list) MOZ_FORMAT_PRINTF(2, 0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-03-29 22:38:53 +03:00
|
|
|
/* Fast paths for formatting integers as though by %d, %o, %u, or %x.
|
|
|
|
Since octal and hex formatting always treat numbers as unsigned, there
|
|
|
|
are no signed overloads for AppendInt{Oct,Hex}. */
|
|
|
|
bool MFBT_API appendIntDec(int32_t);
|
|
|
|
bool MFBT_API appendIntDec(uint32_t);
|
|
|
|
bool MFBT_API appendIntOct(uint32_t);
|
|
|
|
bool MFBT_API appendIntHex(uint32_t);
|
|
|
|
bool MFBT_API appendIntDec(int64_t);
|
|
|
|
bool MFBT_API appendIntDec(uint64_t);
|
|
|
|
bool MFBT_API appendIntOct(uint64_t);
|
|
|
|
bool MFBT_API appendIntHex(uint64_t);
|
|
|
|
|
2021-03-11 02:52:40 +03:00
|
|
|
inline size_t emitted() { return mEmitted; }
|
|
|
|
|
2017-01-13 20:25:59 +03:00
|
|
|
protected:
|
2017-03-13 19:51:10 +03:00
|
|
|
MFBT_API PrintfTarget();
|
2020-03-10 11:47:24 +03:00
|
|
|
virtual ~PrintfTarget() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-01-13 20:25:59 +03:00
|
|
|
/* Subclasses override this. It is called when more output is
|
|
|
|
available. It may be called with len==0. This should return
|
|
|
|
true on success, or false on failure. */
|
|
|
|
virtual bool append(const char* sp, size_t len) = 0;
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
private:
|
2017-01-13 20:25:59 +03:00
|
|
|
/* Number of bytes emitted so far. */
|
|
|
|
size_t mEmitted;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-01-13 20:25:59 +03:00
|
|
|
/* The implementation calls this to emit bytes and update
|
|
|
|
mEmitted. */
|
|
|
|
bool emit(const char* sp, size_t len) {
|
|
|
|
mEmitted += len;
|
|
|
|
return append(sp, len);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:25:59 +03:00
|
|
|
bool fill2(const char* src, int srclen, int width, int flags);
|
|
|
|
bool fill_n(const char* src, int srclen, int width, int prec, int type,
|
|
|
|
int flags);
|
|
|
|
bool cvt_l(long num, int width, int prec, int radix, int type, int flags,
|
2021-02-03 00:18:06 +03:00
|
|
|
const char* hexp);
|
2017-01-13 20:25:59 +03:00
|
|
|
bool cvt_ll(int64_t num, int width, int prec, int radix, int type, int flags,
|
|
|
|
const char* hexp);
|
2021-01-29 07:25:54 +03:00
|
|
|
bool cvt_f(double d, char c, int width, int prec, int flags);
|
2017-01-13 20:25:59 +03:00
|
|
|
bool cvt_s(const char* s, int width, int prec, int flags);
|
|
|
|
};
|
|
|
|
|
2017-03-03 18:17:27 +03:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename AllocPolicy = mozilla::MallocAllocPolicy>
|
|
|
|
struct AllocPolicyBasedFreePolicy {
|
|
|
|
void operator()(const void* ptr) {
|
|
|
|
AllocPolicy policy;
|
|
|
|
policy.free_(const_cast<void*>(ptr));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
// The type returned by Smprintf and friends.
|
|
|
|
template <typename AllocPolicy>
|
|
|
|
using SmprintfPolicyPointer =
|
|
|
|
mozilla::UniquePtr<char, detail::AllocPolicyBasedFreePolicy<AllocPolicy>>;
|
|
|
|
|
|
|
|
// The default type if no alloc policy is specified.
|
|
|
|
typedef SmprintfPolicyPointer<mozilla::MallocAllocPolicy> SmprintfPointer;
|
|
|
|
|
2017-01-13 20:25:59 +03:00
|
|
|
// Used in the implementation of Smprintf et al.
|
|
|
|
template <typename AllocPolicy>
|
2017-02-27 23:29:54 +03:00
|
|
|
class MOZ_STACK_CLASS SprintfState final : private mozilla::PrintfTarget,
|
|
|
|
private AllocPolicy {
|
2017-01-13 20:25:59 +03:00
|
|
|
public:
|
|
|
|
explicit SprintfState(char* base)
|
|
|
|
: mMaxlen(base ? strlen(base) : 0),
|
|
|
|
mBase(base),
|
|
|
|
mCur(base ? base + mMaxlen : 0) {}
|
|
|
|
|
|
|
|
~SprintfState() { this->free_(mBase); }
|
|
|
|
|
2017-05-04 21:10:19 +03:00
|
|
|
bool vprint(const char* format, va_list ap_list) MOZ_FORMAT_PRINTF(2, 0) {
|
2017-02-27 23:29:54 +03:00
|
|
|
// The "" here has a single \0 character, which is what we're
|
|
|
|
// trying to append.
|
|
|
|
return mozilla::PrintfTarget::vprint(format, ap_list) && append("", 1);
|
|
|
|
}
|
|
|
|
|
2017-03-03 18:17:27 +03:00
|
|
|
SmprintfPolicyPointer<AllocPolicy> release() {
|
|
|
|
SmprintfPolicyPointer<AllocPolicy> result(mBase);
|
2017-01-13 20:25:59 +03:00
|
|
|
mBase = nullptr;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool append(const char* sp, size_t len) override {
|
|
|
|
ptrdiff_t off;
|
|
|
|
char* newbase;
|
|
|
|
size_t newlen;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-01-13 20:25:59 +03:00
|
|
|
off = mCur - mBase;
|
|
|
|
if (off + len >= mMaxlen) {
|
|
|
|
/* Grow the buffer */
|
|
|
|
newlen = mMaxlen + ((len > 32) ? len : 32);
|
2018-07-21 02:58:34 +03:00
|
|
|
newbase = this->template maybe_pod_malloc<char>(newlen);
|
2017-01-13 20:25:59 +03:00
|
|
|
if (!newbase) {
|
|
|
|
/* Ran out of memory */
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-21 02:58:34 +03:00
|
|
|
memcpy(newbase, mBase, mMaxlen);
|
|
|
|
this->free_(mBase);
|
2017-01-13 20:25:59 +03:00
|
|
|
mBase = newbase;
|
|
|
|
mMaxlen = newlen;
|
|
|
|
mCur = mBase + off;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy data */
|
|
|
|
memcpy(mCur, sp, len);
|
|
|
|
mCur += len;
|
|
|
|
MOZ_ASSERT(size_t(mCur - mBase) <= mMaxlen);
|
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-01-13 20:25:59 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
private:
|
2017-01-13 20:25:59 +03:00
|
|
|
size_t mMaxlen;
|
|
|
|
char* mBase;
|
|
|
|
char* mCur;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** sprintf into a malloc'd buffer. Return a pointer to the malloc'd
|
|
|
|
** buffer on success, nullptr on failure. Call AllocPolicy::free_ to release
|
|
|
|
** the memory returned.
|
|
|
|
*/
|
|
|
|
template <typename AllocPolicy = mozilla::MallocAllocPolicy>
|
|
|
|
MOZ_FORMAT_PRINTF(1, 2)
|
2017-03-03 18:17:27 +03:00
|
|
|
SmprintfPolicyPointer<AllocPolicy> Smprintf(const char* fmt, ...) {
|
2017-01-13 20:25:59 +03:00
|
|
|
SprintfState<AllocPolicy> ss(nullptr);
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
bool r = ss.vprint(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (!r) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ss.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** "append" sprintf into a malloc'd buffer. "last" is the last value of
|
|
|
|
** the malloc'd buffer. sprintf will append data to the end of last,
|
|
|
|
** growing it as necessary using realloc. If last is nullptr, SmprintfAppend
|
|
|
|
** will allocate the initial string. The return value is the new value of
|
|
|
|
** last for subsequent calls, or nullptr if there is a malloc failure.
|
|
|
|
*/
|
|
|
|
template <typename AllocPolicy = mozilla::MallocAllocPolicy>
|
|
|
|
MOZ_FORMAT_PRINTF(2, 3)
|
2017-03-03 18:17:27 +03:00
|
|
|
SmprintfPolicyPointer<AllocPolicy> SmprintfAppend(
|
|
|
|
SmprintfPolicyPointer<AllocPolicy>&& last, const char* fmt, ...) {
|
|
|
|
SprintfState<AllocPolicy> ss(last.release());
|
2017-01-13 20:25:59 +03:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
bool r = ss.vprint(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (!r) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ss.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** va_list forms of the above.
|
|
|
|
*/
|
|
|
|
template <typename AllocPolicy = mozilla::MallocAllocPolicy>
|
2017-05-04 21:10:19 +03:00
|
|
|
MOZ_FORMAT_PRINTF(1, 0)
|
2017-03-03 18:17:27 +03:00
|
|
|
SmprintfPolicyPointer<AllocPolicy> Vsmprintf(const char* fmt, va_list ap) {
|
2017-01-13 20:25:59 +03:00
|
|
|
SprintfState<AllocPolicy> ss(nullptr);
|
|
|
|
if (!ss.vprint(fmt, ap)) return nullptr;
|
|
|
|
return ss.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AllocPolicy = mozilla::MallocAllocPolicy>
|
2017-05-04 21:10:19 +03:00
|
|
|
MOZ_FORMAT_PRINTF(2, 0)
|
2017-03-03 18:17:27 +03:00
|
|
|
SmprintfPolicyPointer<AllocPolicy> VsmprintfAppend(
|
|
|
|
SmprintfPolicyPointer<AllocPolicy>&& last, const char* fmt, va_list ap) {
|
|
|
|
SprintfState<AllocPolicy> ss(last.release());
|
2017-01-13 20:25:59 +03:00
|
|
|
if (!ss.vprint(fmt, ap)) return nullptr;
|
|
|
|
return ss.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_Printf_h */
|