NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <algorithm>
9#include <iomanip>
10#include <sstream>
11#include <string>
12#include <string_view>
13#include <vector>
14
16
17namespace nnl {
22namespace utl::string {
36template <typename T>
37std::string PrependZero(T num, std::size_t digits = 2) {
38 static_assert(std::is_integral_v<T>, "T must be integral");
39 NNL_EXPECTS_DBG(num >= 0);
40 std::string str = std::to_string(num);
41 return std::string(digits - std::min(digits, str.length()), '0') + str;
42}
43
49bool EndsWith(std::string_view str, std::string_view suffix);
56bool StartsWith(std::string_view str, std::string_view prefix);
63std::string Join(const std::vector<std::string>& strings, std::string delim = ",");
70std::vector<std::string> Split(std::string_view str, std::string delim = ",");
77std::string Truncate(std::string_view str, std::size_t n);
78
85std::string FloatToString(float value, int n = 6);
94template <typename T>
95std::string IntToHex(T i, bool prefix = false, bool prepend = true) {
96 static_assert(std::is_integral_v<T>);
97
98 std::stringstream stream;
99
100 if (prefix) stream << "0x";
101
102 if (prepend) stream << std::setfill('0') << std::setw(sizeof(T) * 2);
103
104 stream << std::hex;
105
106 if constexpr (sizeof(i) < sizeof(int))
107 stream << static_cast<int>(i);
108 else
109 stream << i;
110
111 return stream.str();
112}
113
121std::string ToLower(std::string_view str);
122
134bool CompareNat(std::string_view a, std::string_view b);
135
137} // namespace utl::string
138} // namespace nnl
Defines macros for Design-by-Contract verification.
#define NNL_EXPECTS_DBG(precondition)
Debug-only precondition check.
Definition contract.hpp:59
bool CompareNat(std::string_view a, std::string_view b)
Performs natural string comparison for sorting.
bool EndsWith(std::string_view str, std::string_view suffix)
Checks if a string ends with a specific suffix.
std::string Join(const std::vector< std::string > &strings, std::string delim=",")
Joins a container of strings into one string using a delimiter.
bool StartsWith(std::string_view str, std::string_view prefix)
Checks if a string starts with a specific prefix.
std::string PrependZero(T num, std::size_t digits=2)
Converts a number to a string padded with zero's.
Definition string.hpp:37
std::string IntToHex(T i, bool prefix=false, bool prepend=true)
Converts an integer to a hexadecimal string.
Definition string.hpp:95
std::string ToLower(std::string_view str)
Converts a string to lowercase.
std::vector< std::string > Split(std::string_view str, std::string delim=",")
Splits a string by delimiter.
std::string Truncate(std::string_view str, std::size_t n)
Truncates a string to specified length.
std::string FloatToString(float value, int n=6)
Converts a float value to a string with fixed precision.
Contains various utility functions for working with strings.
Definition string.hpp:22
Definition exception.hpp:56