/string_format.h

string_format("Super {}", "Easy")

Primary LanguageCMakeBSD Zero Clause License0BSD

#include <string_format.h>

#include <string_format.h>

void Example() {
    auto text = string_format("Super {}", "Easy");
}

What?

Simply passes the arguments to std::format() or fmt::format().

Installation

xmake

xmake.lua

add_repositories("MrowrLib https://github.com/MrowrLib/Packages.git")
add_requires("string_format")

-- If you're using C++20, you won't need fmt
set_languages("c++20")

-- Else, add 'fmt' as a dependency
add_requires("fmt")

target("Example")
    add_packages("string_format")
    add_packages("fmt") -- if using 'fmt'

vcpkg

CMakeLists.txt

add_executable(Example main.cpp)

# Find string_format and link it to your target
find_package(string_format CONFIG REQUIRED)
target_link_libraries(Example PRIVATE string_format::string_format)

# If you're using C++20, you won't need fmt
target_compile_features(Example PRIVATE cxx_std_20)

# Else, add 'fmt' as a dependency
find_package(fmt CONFIG REQUIRED)
target_link_libraries(Example PRIVATE fmt::fmt)

vcpkg.json

{
    "dependencies": ["mrowr-string-format"]
}

And if you want to use fmt:

{
    "dependencies": ["mrowr-string-format", "fmt"]
}

vcpkg-configuration.json

{
    "default-registry": {
        "kind": "git",
        "repository": "https://github.com/microsoft/vcpkg.git",
        "baseline": "95252eadd63118201b0d0df0b4360fa613f0de84"
    },
    "registries": [
        {
            "kind": "git",
            "repository": "https://github.com/MrowrLib/Packages.git",
            "baseline": "a10d9e3ed79e875558cfb1aa3cd012a71a102815",
            "packages": ["mrowr-string-format"]
        }
    ]
}

Update the default-registry baseline to the latest commit from https://github.com/microsoft/vcpkg
Update the MrowrLib/Packages baseline to the latest commit from https://github.com/MrowrLib/Packages

Why?

I got really tired of switching between fmt::format and std::format.

e.g. because only certain C++ versions and compilers support std::format.

How?

If fmt headers are detected, then fmt::format is used.

Else std::format is used (which will explode in C++ versions below C++20).

If you want to force use of fmt, then define STRING_FORMAT_USE_FMT before including the header.

If you want to force use of std::format, then define STRING_FORMAT_USE_STD before including the header.

License

Use however, no attribution required.

BSD Zero Clause License (SPDX: 0BSD)

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.