Code Monkey home page Code Monkey logo

concat_string's Introduction

concat_str

CircleCI Build status

concat_str is a utility function for concatenating multiple string types while minimizing potential free store allocations.

Illustration of the problem

Potentially multiple reallocations:

using namespace std::string_literals;
// Pretend this is concatenating dynamic strings from functions
auto const s = "Hello world!"s + ' ' + "This is C++."s + ' ' + "Here is a string.";

Instead use a function that will calculate the total length and reserve the string memory once:

// Pretend this is concatenating dynamic strings from functions
auto const s = zpp::concat_string("Hello world!", ' ', "This is C++.", ' ', "Here is a string.");

API

Main convenience functions:

  • std::string concat_string(...)
  • std::wstring concat_wstring(...)
  • std::u8string concat_u8string(...) (requires C++20)
  • std::u16string concat_u16string(...)
  • std::u32string concat_u32string(...)

Generic template function:

  • template <typename CharT, typename Traits, typename Alloc, typename... Ts>
    std::basic_string<CharT, Traits, Alloc> concat_basic_string(...)

Supported types

Requires that the underlying character types are all the same. I.e., no mixing char and wchar_t.

  • Single character
    • char
    • wchar_t
    • char8_t (requires C++20)
    • char16_t
    • char32_t
  • String literal
    • ""
    • L""
    • u8""
    • u""
    • U""
  • std::basic_string<...>
    • std::string
    • std::wstring
    • std::u8string (requires C++20)
    • std::u16string
    • std::u32string
  • std::basic_string_view<...> (requires C++17)
    • std::string_view (requires C++17)
    • std::wstring_view (requires C++17)
    • std::u8string_view (requires C++20)
    • std::u16string_view (requires C++17)
    • std::u32string_view (requires C++17)
  • std::initializer_list<CharT>

Requirements

C++11 with variadic template support

concat_string's People

Contributors

zachstruck avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.