Serenity Operating System
at portability 115 lines 3.2 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/PrintfImplementation.h> 28#include <AK/StdLibExtras.h> 29#include <AK/StringBuilder.h> 30#include <AK/String.h> 31 32namespace AK { 33 34inline void StringBuilder::will_append(size_t size) 35{ 36 if ((m_length + size) > (size_t)m_buffer.size()) 37 m_buffer.grow(max((size_t)16, (size_t)m_buffer.size() * 2 + size)); 38} 39 40StringBuilder::StringBuilder(size_t initial_capacity) 41{ 42 m_buffer.grow((int)initial_capacity); 43} 44 45void StringBuilder::append(const StringView& str) 46{ 47 if (str.is_empty()) 48 return; 49 will_append(str.length()); 50 memcpy(m_buffer.data() + m_length, str.characters_without_null_termination(), str.length()); 51 m_length += str.length(); 52} 53 54void StringBuilder::append(const char* characters, size_t length) 55{ 56 if (!length) 57 return; 58 will_append(length); 59 memcpy(m_buffer.data() + m_length, characters, length); 60 m_length += length; 61} 62 63void StringBuilder::append(char ch) 64{ 65 will_append(1); 66 m_buffer.data()[m_length] = ch; 67 m_length += 1; 68} 69 70void StringBuilder::appendvf(const char* fmt, va_list ap) 71{ 72 printf_internal([this](char*&, char ch) { 73 append(ch); 74 }, 75 nullptr, fmt, ap); 76} 77 78void StringBuilder::appendf(const char* fmt, ...) 79{ 80 va_list ap; 81 va_start(ap, fmt); 82 will_append(strlen(fmt)); 83 appendvf(fmt, ap); 84 va_end(ap); 85} 86 87ByteBuffer StringBuilder::to_byte_buffer() 88{ 89 m_buffer.trim(m_length); 90 m_length = 0; 91 return move(m_buffer); 92} 93 94String StringBuilder::to_string() 95{ 96 return String((const char*)m_buffer.data(), m_length); 97} 98 99String StringBuilder::build() 100{ 101 return to_string(); 102} 103 104StringView StringBuilder::string_view() const 105{ 106 return StringView { (const char*)m_buffer.data(), m_length }; 107} 108 109void StringBuilder::clear() 110{ 111 m_buffer.clear(); 112 m_length = 0; 113} 114 115}