Printf.cpp (1765B)
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/. */ 6 7 /* 8 * Portable safe sprintf code. 9 * 10 * Author: Kipp E.B. Hickman 11 */ 12 13 #include "js/Printf.h" 14 15 #include "mozilla/Printf.h" 16 17 #include "js/AllocPolicy.h" 18 19 using namespace js; 20 21 using JSSmprintfPointer = mozilla::SmprintfPolicyPointer<js::SystemAllocPolicy>; 22 23 JS_PUBLIC_API JS::UniqueChars JS_smprintf(const char* fmt, ...) { 24 va_list ap; 25 va_start(ap, fmt); 26 JSSmprintfPointer result = mozilla::Vsmprintf<js::SystemAllocPolicy>(fmt, ap); 27 va_end(ap); 28 return JS::UniqueChars(result.release()); 29 } 30 31 JS_PUBLIC_API JS::UniqueChars JS_sprintf_append(JS::UniqueChars&& last, 32 const char* fmt, ...) { 33 va_list ap; 34 va_start(ap, fmt); 35 JSSmprintfPointer lastPtr(last.release()); 36 JSSmprintfPointer result = mozilla::VsmprintfAppend<js::SystemAllocPolicy>( 37 std::move(lastPtr), fmt, ap); 38 va_end(ap); 39 return JS::UniqueChars(result.release()); 40 } 41 42 JS_PUBLIC_API JS::UniqueChars JS_vsmprintf(const char* fmt, va_list ap) { 43 return JS::UniqueChars( 44 mozilla::Vsmprintf<js::SystemAllocPolicy>(fmt, ap).release()); 45 } 46 47 JS_PUBLIC_API JS::UniqueChars JS_vsprintf_append(JS::UniqueChars&& last, 48 const char* fmt, va_list ap) { 49 JSSmprintfPointer lastPtr(last.release()); 50 return JS::UniqueChars(mozilla::VsmprintfAppend<js::SystemAllocPolicy>( 51 std::move(lastPtr), fmt, ap) 52 .release()); 53 }