nfx-stringutils 0.7.0
Modern C++20 header-only library providing high-performance string utilities and zero-allocation splitting
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
31
32#pragma once
33
34#include <cstdint>
35#include <optional>
36#include <string>
37#include <string_view>
38
39namespace nfx::string
40{
41 //=====================================================================
42 // String utilities
43 //=====================================================================
44
45 //----------------------------------------------
46 // String validation
47 //----------------------------------------------
48
56 [[nodiscard]] inline constexpr bool hasExactLength( std::string_view str, std::size_t expectedLength ) noexcept;
57
64 [[nodiscard]] inline constexpr bool isEmpty( std::string_view str ) noexcept;
65
73 [[nodiscard]] inline constexpr bool isNullOrWhiteSpace( std::string_view str ) noexcept;
74
82 [[nodiscard]] inline constexpr bool isAllDigits( std::string_view str ) noexcept;
83
84 //----------------------------------------------
85 // Character classification
86 //----------------------------------------------
87
94 [[nodiscard]] inline constexpr bool isWhitespace( char c ) noexcept;
95
102 [[nodiscard]] inline constexpr bool isDigit( char c ) noexcept;
103
110 [[nodiscard]] inline constexpr bool isAlpha( char c ) noexcept;
111
118 [[nodiscard]] inline constexpr bool isAlphaNumeric( char c ) noexcept;
119
126 [[nodiscard]] inline constexpr bool isHexDigit( char c ) noexcept;
127
134 [[nodiscard]] inline constexpr bool isOctal( char c ) noexcept;
135
136 //----------------------------------------------
137 // String operations
138 //----------------------------------------------
139
147 [[nodiscard]] inline constexpr bool startsWith( std::string_view str, std::string_view prefix ) noexcept;
148
156 [[nodiscard]] inline constexpr bool startsWith( std::string_view str, char prefix ) noexcept;
157
165 [[nodiscard]] inline constexpr bool startsWith( std::string_view str, const char* prefix ) noexcept;
166
176 [[nodiscard]] inline bool istartsWith( std::string_view str, std::string_view prefix ) noexcept;
177
187 [[nodiscard]] inline bool istartsWith( std::string_view str, char prefix ) noexcept;
188
198 [[nodiscard]] inline bool istartsWith( std::string_view str, const char* prefix ) noexcept;
199
207 [[nodiscard]] inline constexpr bool endsWith( std::string_view str, std::string_view suffix ) noexcept;
208
216 [[nodiscard]] inline constexpr bool endsWith( std::string_view str, char suffix ) noexcept;
217
225 [[nodiscard]] inline constexpr bool endsWith( std::string_view str, const char* suffix ) noexcept;
226
236 [[nodiscard]] inline bool iendsWith( std::string_view str, std::string_view suffix ) noexcept;
237
247 [[nodiscard]] inline bool iendsWith( std::string_view str, char suffix ) noexcept;
248
258 [[nodiscard]] inline bool iendsWith( std::string_view str, const char* suffix ) noexcept;
259
267 [[nodiscard]] inline constexpr bool contains( std::string_view str, std::string_view substr ) noexcept;
268
276 [[nodiscard]] inline constexpr bool contains( std::string_view str, char ch ) noexcept;
277
285 [[nodiscard]] inline constexpr bool contains( std::string_view str, const char* substr ) noexcept;
286
296 [[nodiscard]] inline bool icontains( std::string_view str, std::string_view substr ) noexcept;
297
307 [[nodiscard]] inline bool icontains( std::string_view str, char ch ) noexcept;
308
318 [[nodiscard]] inline bool icontains( std::string_view str, const char* substr ) noexcept;
319
327 [[nodiscard]] inline constexpr bool equals( std::string_view lhs, std::string_view rhs ) noexcept;
328
336 [[nodiscard]] inline bool iequals( std::string_view lhs, std::string_view rhs ) noexcept;
337
346 [[nodiscard]] inline std::size_t count( std::string_view str, std::string_view substr ) noexcept;
347
357 [[nodiscard]] inline std::size_t countOverlapping( std::string_view str, std::string_view substr ) noexcept;
358
366 [[nodiscard]] inline constexpr std::size_t count( std::string_view str, char ch ) noexcept;
367
377 [[nodiscard]] inline std::string replace( std::string_view str, std::string_view oldStr, std::string_view newStr );
378
388 [[nodiscard]] inline std::string replaceAll( std::string_view str, std::string_view oldStr, std::string_view newStr );
389
399 template <typename Container>
400 [[nodiscard]] inline std::string join( const Container& elements, std::string_view delimiter );
401
412 template <typename Iterator>
413 [[nodiscard]] inline std::string join( Iterator begin, Iterator end, std::string_view delimiter );
414
423 [[nodiscard]] inline std::string reverse( std::string_view str );
424
434 [[nodiscard]] inline constexpr std::size_t indexOf( std::string_view str, std::string_view substr ) noexcept;
435
445 [[nodiscard]] inline constexpr std::size_t indexOf( std::string_view str, char ch ) noexcept;
446
456 [[nodiscard]] inline constexpr std::size_t lastIndexOf( std::string_view str, std::string_view substr ) noexcept;
457
467 [[nodiscard]] inline constexpr std::size_t lastIndexOf( std::string_view str, char ch ) noexcept;
468
469 //----------------------------------------------
470 // String formatting and padding
471 //----------------------------------------------
472
482 [[nodiscard]] inline std::string padLeft( std::string_view str, std::size_t width, char fillChar = ' ' );
483
493 [[nodiscard]] inline std::string padRight( std::string_view str, std::size_t width, char fillChar = ' ' );
494
505 [[nodiscard]] inline std::string center( std::string_view str, std::size_t width, char fillChar = ' ' );
506
515 [[nodiscard]] inline std::string repeat( std::string_view str, std::size_t count );
516
517 //----------------------------------------------
518 // Substring extraction
519 //----------------------------------------------
520
529 [[nodiscard]] inline constexpr std::string_view substringBefore( std::string_view str, std::string_view delimiter ) noexcept;
530
539 [[nodiscard]] inline constexpr std::string_view substringBefore( std::string_view str, char delimiter ) noexcept;
540
549 [[nodiscard]] inline constexpr std::string_view substringAfter( std::string_view str, std::string_view delimiter ) noexcept;
550
559 [[nodiscard]] inline constexpr std::string_view substringAfter( std::string_view str, char delimiter ) noexcept;
560
569 [[nodiscard]] inline constexpr std::string_view substringBeforeLast( std::string_view str, std::string_view delimiter ) noexcept;
570
579 [[nodiscard]] inline constexpr std::string_view substringBeforeLast( std::string_view str, char delimiter ) noexcept;
580
589 [[nodiscard]] inline constexpr std::string_view substringAfterLast( std::string_view str, std::string_view delimiter ) noexcept;
590
599 [[nodiscard]] inline constexpr std::string_view substringAfterLast( std::string_view str, char delimiter ) noexcept;
600
611 [[nodiscard]] inline constexpr std::string_view extractBetween( std::string_view str, std::string_view start, std::string_view end ) noexcept;
612
621 [[nodiscard]] inline constexpr std::string_view removePrefix( std::string_view str, std::string_view prefix ) noexcept;
622
631 [[nodiscard]] inline constexpr std::string_view removeSuffix( std::string_view str, std::string_view suffix ) noexcept;
632
633 //----------------------------------------------
634 // Character & String Removal
635 //----------------------------------------------
636
645 [[nodiscard]] inline std::string removeAll( std::string_view str, char ch );
646
656 [[nodiscard]] inline std::string removeAll( std::string_view str, std::string_view substr );
657
667 template <typename Predicate>
668 [[nodiscard]] inline std::string removeIf( std::string_view str, Predicate pred );
669
678 [[nodiscard]] inline std::string removeWhitespace( std::string_view str );
679
689 [[nodiscard]] inline std::string collapseWhitespace( std::string_view str );
690
691 //----------------------------------------------
692 // String trimming
693 //----------------------------------------------
694
702 [[nodiscard]] inline constexpr std::string_view trimStart( std::string_view str ) noexcept;
703
711 [[nodiscard]] inline constexpr std::string_view trimEnd( std::string_view str ) noexcept;
712
720 [[nodiscard]] inline constexpr std::string_view trim( std::string_view str ) noexcept;
721
722 //----------------------------------------------
723 // Predicate-based operations
724 //----------------------------------------------
725
736 template <typename Predicate>
737 [[nodiscard]] inline constexpr std::string_view trimStartIf( std::string_view str, Predicate pred ) noexcept;
738
749 template <typename Predicate>
750 [[nodiscard]] inline constexpr std::string_view trimEndIf( std::string_view str, Predicate pred ) noexcept;
751
762 template <typename Predicate>
763 [[nodiscard]] inline constexpr std::string_view trimIf( std::string_view str, Predicate pred ) noexcept;
764
774 template <typename Predicate>
775 [[nodiscard]] inline constexpr std::size_t countIf( std::string_view str, Predicate pred ) noexcept;
776
786 template <typename Predicate>
787 [[nodiscard]] inline constexpr std::size_t findIf( std::string_view str, Predicate pred ) noexcept;
788
798 template <typename Predicate>
799 [[nodiscard]] inline constexpr std::size_t findIfNot( std::string_view str, Predicate pred ) noexcept;
800
812 template <typename Predicate>
813 [[nodiscard]] inline std::string replaceIf( std::string_view str, Predicate pred, char replacement );
814
815 //----------------------------------------------
816 // String case conversion
817 //----------------------------------------------
818
827 [[nodiscard]] inline std::string toLower( std::string_view str );
828
837 [[nodiscard]] inline std::string toUpper( std::string_view str );
838
839 //----------------------------------------------
840 // Character case conversion
841 //----------------------------------------------
842
850 [[nodiscard]] inline constexpr char toLower( char c ) noexcept;
851
859 [[nodiscard]] inline constexpr char toUpper( char c ) noexcept;
860
861 //----------------------------------------------
862 // Character conversion
863 //----------------------------------------------
864
873 [[nodiscard]] inline constexpr int hexToInt( char c ) noexcept;
874
881 [[nodiscard]] inline constexpr int octalToInt( char c ) noexcept;
882
889 [[nodiscard]] inline constexpr int digitToInt( char c ) noexcept;
890
891 //----------------------------------------------
892 // UTF-8 utilities
893 //----------------------------------------------
894
908 [[nodiscard]] inline bool decodeUtf8Codepoint( std::string_view str, std::size_t& i, uint32_t& codepoint ) noexcept;
909
921 inline void encodeUtf8Codepoint( std::string& result, uint32_t codepoint ) noexcept;
922
930 [[nodiscard]] inline std::size_t utf8Length( std::string_view str ) noexcept;
931
942 [[nodiscard]] inline bool isValidUtf8( std::string_view str ) noexcept;
943
956 [[nodiscard]] inline std::string_view utf8Substring( std::string_view str,
957 std::size_t startCodepoint,
958 std::size_t codepointCount = std::string_view::npos ) noexcept;
959
960 //----------------------------------------------
961 // String parsing
962 //----------------------------------------------
963
971 template <typename T>
972 requires(
973 std::is_same_v<std::decay_t<T>, bool> ||
974 std::is_same_v<std::decay_t<T>, int> ||
975 std::is_same_v<std::decay_t<T>, std::uint32_t> ||
976 std::is_same_v<std::decay_t<T>, std::int64_t> ||
977 std::is_same_v<std::decay_t<T>, std::uint64_t> ||
978 std::is_same_v<std::decay_t<T>, float> ||
979 std::is_same_v<std::decay_t<T>, double> )
980 [[nodiscard]] inline bool fromString( std::string_view str, T& result ) noexcept;
981
988 template <typename T>
989 requires(
990 std::is_same_v<std::decay_t<T>, bool> ||
991 std::is_same_v<std::decay_t<T>, int> ||
992 std::is_same_v<std::decay_t<T>, std::uint32_t> ||
993 std::is_same_v<std::decay_t<T>, std::int64_t> ||
994 std::is_same_v<std::decay_t<T>, std::uint64_t> ||
995 std::is_same_v<std::decay_t<T>, float> ||
996 std::is_same_v<std::decay_t<T>, double> )
997 [[nodiscard]] inline std::optional<T> fromString( std::string_view str ) noexcept;
998
999 //----------------------------------------------
1000 // Network and URI validation
1001 //----------------------------------------------
1002
1003 //-----------------------------
1004 // URI character classification
1005 //-----------------------------
1006
1013 [[nodiscard]] inline constexpr bool isUriReserved( char c ) noexcept;
1014
1022 [[nodiscard]] inline constexpr bool isUriReserved( std::string_view str ) noexcept;
1023
1030 [[nodiscard]] inline constexpr bool isUriUnreserved( char c ) noexcept;
1031
1039 [[nodiscard]] inline constexpr bool isUriUnreserved( std::string_view str ) noexcept;
1040
1041 //-----------------------------
1042 // URL encoding/decoding
1043 //-----------------------------
1044
1054 [[nodiscard]] inline std::string urlEncode( std::string_view str );
1055
1065 [[nodiscard]] inline std::string urlDecode( std::string_view str ) noexcept;
1066
1067 //-----------------------------
1068 // JSON escape/unescape
1069 //-----------------------------
1070
1084 [[nodiscard]] inline std::string jsonEscape( std::string_view str, bool escapeNonAscii = false, bool escapeForHtml = false );
1085
1096 [[nodiscard]] inline std::string jsonUnescape( std::string_view str ) noexcept;
1097
1098 //-----------------------------
1099 // XML/HTML escape/unescape
1100 //-----------------------------
1101
1117 [[nodiscard]] inline std::string xmlEscape( std::string_view str, bool escapeNonAscii = false );
1118
1129 [[nodiscard]] inline std::string xmlUnescape( std::string_view str ) noexcept;
1130
1131 //-----------------------------
1132 // C/C++ escape sequences
1133 //-----------------------------
1134
1147 [[nodiscard]] inline std::string cppEscape( std::string_view str );
1148
1160 [[nodiscard]] inline std::string cppUnescape( std::string_view str ) noexcept;
1161
1162 //-----------------------------
1163 // String formatting utilities
1164 //-----------------------------
1165
1175 [[nodiscard]] inline std::string truncate( std::string_view str, size_t maxLength );
1176
1188 [[nodiscard]] inline std::string truncate( std::string_view str, size_t maxLength, std::string_view ellipsis );
1189
1201 [[nodiscard]] inline std::string wordWrap( std::string_view str, size_t width );
1202
1213 [[nodiscard]] inline std::string indent( std::string_view str, size_t spaces );
1214
1224 [[nodiscard]] inline std::string dedent( std::string_view str );
1225
1226 //-----------------------------
1227 // Advanced comparison
1228 //-----------------------------
1229
1239 [[nodiscard]] inline constexpr int compareIgnoreCase( std::string_view lhs, std::string_view rhs ) noexcept;
1240
1251 [[nodiscard]] inline constexpr int naturalCompare( std::string_view lhs, std::string_view rhs ) noexcept;
1252
1264 [[nodiscard]] inline constexpr int inaturalCompare( std::string_view lhs, std::string_view rhs ) noexcept;
1265
1276 [[nodiscard]] inline constexpr bool naturalSort( std::string_view a, std::string_view b ) noexcept;
1277
1290 [[nodiscard]] inline constexpr bool inaturalSort( std::string_view a, std::string_view b ) noexcept;
1291
1301 [[nodiscard]] inline constexpr std::string_view commonPrefix( std::string_view lhs, std::string_view rhs ) noexcept;
1302
1312 [[nodiscard]] inline constexpr std::string_view commonSuffix( std::string_view lhs, std::string_view rhs ) noexcept;
1313
1314 //-----------------------------
1315 // IP address validation
1316 //-----------------------------
1317
1325 [[nodiscard]] inline constexpr bool isIpv4Address( std::string_view str ) noexcept;
1326
1334 [[nodiscard]] inline constexpr bool isIpv6Address( std::string_view str ) noexcept;
1335
1336 //-----------------------------
1337 // Host validation
1338 //-----------------------------
1339
1349 [[nodiscard]] inline constexpr bool isHostname( std::string_view str ) noexcept;
1350
1358 [[nodiscard]] inline constexpr bool isIdnHostname( std::string_view str ) noexcept;
1359
1368 [[nodiscard]] inline constexpr bool isDomainName( std::string_view str ) noexcept;
1369
1370 //-----------------------------
1371 // Port validation
1372 //-----------------------------
1373
1382 [[nodiscard]] inline constexpr bool isPortNumber( std::string_view str ) noexcept;
1383
1384 //-----------------------------
1385 // Endpoint parsing
1386 //-----------------------------
1387
1399 [[nodiscard]] inline bool tryParseEndpoint( std::string_view endpoint,
1400 std::string_view& host,
1401 uint16_t& port ) noexcept;
1402
1403 //-----------------------------
1404 // Date and Time validation (RFC 3339)
1405 //-----------------------------
1406
1416 [[nodiscard]] inline constexpr bool isDateTime( std::string_view str ) noexcept;
1417
1426 [[nodiscard]] inline constexpr bool isDate( std::string_view str ) noexcept;
1427
1436 [[nodiscard]] inline constexpr bool isTime( std::string_view str ) noexcept;
1437
1446 [[nodiscard]] inline constexpr bool isDuration( std::string_view str ) noexcept;
1447
1448 //-----------------------------
1449 // Email validation (RFC 5321)
1450 //-----------------------------
1451
1461 [[nodiscard]] inline constexpr bool isEmail( std::string_view str ) noexcept;
1462
1470 [[nodiscard]] inline constexpr bool isIdnEmail( std::string_view str ) noexcept;
1471
1472 //-----------------------------
1473 // UUID validation (RFC 4122)
1474 //-----------------------------
1475
1484 [[nodiscard]] inline constexpr bool isUuid( std::string_view str ) noexcept;
1485
1486 //-----------------------------
1487 // URI validation (RFC 3986)
1488 //-----------------------------
1489
1498 [[nodiscard]] inline constexpr bool isUri( std::string_view str ) noexcept;
1499
1508 [[nodiscard]] inline constexpr bool isUriReference( std::string_view str ) noexcept;
1509
1520 [[nodiscard]] inline constexpr bool isUriTemplate( std::string_view str ) noexcept;
1521
1522 //-----------------------------
1523 // IRI validation (RFC 3987)
1524 //-----------------------------
1525
1534 [[nodiscard]] inline constexpr bool isIri( std::string_view str ) noexcept;
1535
1544 [[nodiscard]] inline constexpr bool isIriReference( std::string_view str ) noexcept;
1545
1546 //-----------------------------
1547 // JSON Pointer validation (RFC 6901)
1548 //-----------------------------
1549
1559 [[nodiscard]] inline constexpr bool isJsonPointer( std::string_view str ) noexcept;
1560
1569 [[nodiscard]] inline constexpr bool isRelativeJsonPointer( std::string_view str ) noexcept;
1570} // namespace nfx::string
1571
1572#include "nfx/detail/string/Utils.inl"
constexpr bool isAlphaNumeric(char c) noexcept
Check if character is ASCII alphanumeric.
constexpr int hexToInt(char c) noexcept
Convert a hexadecimal character to its integer value.
std::string truncate(std::string_view str, size_t maxLength)
Truncate string to maximum length.
constexpr std::string_view substringAfter(std::string_view str, std::string_view delimiter) noexcept
Extract substring after first occurrence of delimiter.
std::string toLower(std::string_view str)
Convert string to lowercase.
constexpr std::string_view extractBetween(std::string_view str, std::string_view start, std::string_view end) noexcept
Extract substring between start and end delimiters.
std::string replaceAll(std::string_view str, std::string_view oldStr, std::string_view newStr)
Replace all occurrences of substring with replacement.
constexpr std::string_view removePrefix(std::string_view str, std::string_view prefix) noexcept
Remove prefix from string if present.
constexpr bool inaturalSort(std::string_view a, std::string_view b) noexcept
Case-insensitive natural sort comparison predicate.
constexpr bool isIdnEmail(std::string_view str) noexcept
Validate Internationalized email address format (EAI/SMTPUTF8).
bool isValidUtf8(std::string_view str) noexcept
Validate that a string contains valid UTF-8 encoding.
constexpr bool isIdnHostname(std::string_view str) noexcept
Validate Internationalized Domain Name (IDN) hostname format.
bool iequals(std::string_view lhs, std::string_view rhs) noexcept
Fast case-insensitive string comparison.
constexpr bool isOctal(char c) noexcept
Check if character is ASCII octal digit.
constexpr std::size_t lastIndexOf(std::string_view str, std::string_view substr) noexcept
Find last occurrence of substring.
std::string reverse(std::string_view str)
Reverse a string.
constexpr int inaturalCompare(std::string_view lhs, std::string_view rhs) noexcept
Case-insensitive natural sorting comparison (handles embedded numbers).
constexpr bool isNullOrWhiteSpace(std::string_view str) noexcept
Fast check if string is null, empty, or contains only whitespace.
std::string replaceIf(std::string_view str, Predicate pred, char replacement)
Replace characters matching predicate with replacement character.
constexpr bool isEmpty(std::string_view str) noexcept
Fast check if string is empty.
bool iendsWith(std::string_view str, std::string_view suffix) noexcept
Case-insensitive check if string ends with suffix.
std::string toUpper(std::string_view str)
Convert string to uppercase.
constexpr bool isDuration(std::string_view str) noexcept
Validate ISO 8601 duration format.
bool fromString(std::string_view str, T &result) noexcept
Parse string to type T using output parameter.
bool tryParseEndpoint(std::string_view endpoint, std::string_view &host, uint16_t &port) noexcept
Parse network endpoint into host and port.
constexpr bool isUuid(std::string_view str) noexcept
Validate UUID format (RFC 4122).
std::string repeat(std::string_view str, std::size_t count)
Repeat string specified number of times.
constexpr std::string_view substringAfterLast(std::string_view str, std::string_view delimiter) noexcept
Extract substring after last occurrence of delimiter.
std::string replace(std::string_view str, std::string_view oldStr, std::string_view newStr)
Replace first occurrence of substring with replacement.
constexpr bool equals(std::string_view lhs, std::string_view rhs) noexcept
Fast case-sensitive string comparison.
std::string_view utf8Substring(std::string_view str, std::size_t startCodepoint, std::size_t codepointCount=std::string_view::npos) noexcept
Extract a substring by Unicode codepoint positions.
constexpr std::string_view trimStart(std::string_view str) noexcept
Remove leading whitespace from string.
std::size_t utf8Length(std::string_view str) noexcept
Count the number of Unicode codepoints in a UTF-8 string.
constexpr bool isPortNumber(std::string_view str) noexcept
Validate port number string (RFC 6335).
constexpr bool isEmail(std::string_view str) noexcept
Validate email address format (RFC 5321).
constexpr int octalToInt(char c) noexcept
Convert an octal character to its integer value.
constexpr bool isUriTemplate(std::string_view str) noexcept
Validate URI Template format (RFC 6570).
constexpr bool isTime(std::string_view str) noexcept
Validate RFC 3339 full-time format.
constexpr bool isUriReserved(char c) noexcept
Check if character is URI reserved (RFC 3986 Section 2.2).
constexpr std::string_view trimIf(std::string_view str, Predicate pred) noexcept
Remove leading and trailing characters matching predicate.
constexpr bool isAlpha(char c) noexcept
Check if character is ASCII alphabetic.
std::string join(const Container &elements, std::string_view delimiter)
Join container elements with delimiter.
constexpr bool hasExactLength(std::string_view str, std::size_t expectedLength) noexcept
Fast check if string has exact length.
std::string urlDecode(std::string_view str) noexcept
Decode percent-encoded URL string (RFC 3986).
constexpr bool isDomainName(std::string_view str) noexcept
Validate domain name format (RFC 1035).
bool istartsWith(std::string_view str, std::string_view prefix) noexcept
Case-insensitive check if string starts with prefix.
constexpr bool isIriReference(std::string_view str) noexcept
Validate IRI-reference format (RFC 3987).
constexpr bool isUri(std::string_view str) noexcept
Validate URI format (RFC 3986).
constexpr std::size_t countIf(std::string_view str, Predicate pred) noexcept
Count characters matching predicate.
constexpr bool isRelativeJsonPointer(std::string_view str) noexcept
Validate relative JSON Pointer format.
constexpr std::string_view commonPrefix(std::string_view lhs, std::string_view rhs) noexcept
Find longest common prefix of two strings.
std::string removeAll(std::string_view str, char ch)
Remove all occurrences of a character from string.
constexpr std::size_t indexOf(std::string_view str, std::string_view substr) noexcept
Find first occurrence of substring.
std::string center(std::string_view str, std::size_t width, char fillChar=' ')
Center string within specified width.
constexpr int digitToInt(char c) noexcept
Convert a decimal digit character to its integer value.
std::string removeIf(std::string_view str, Predicate pred)
Remove all characters matching a predicate from string.
void encodeUtf8Codepoint(std::string &result, uint32_t codepoint) noexcept
Encode a Unicode code point to UTF-8.
constexpr bool isDate(std::string_view str) noexcept
Validate RFC 3339 full-date format.
std::string cppEscape(std::string_view str)
Escape string for use as C/C++ string literal.
std::string cppUnescape(std::string_view str) noexcept
Unescape C/C++ string literal escape sequences.
std::string padLeft(std::string_view str, std::size_t width, char fillChar=' ')
Pad string on the left to reach specified width.
constexpr std::string_view trimEnd(std::string_view str) noexcept
Remove trailing whitespace from string.
std::size_t count(std::string_view str, std::string_view substr) noexcept
Count occurrences of substring in string.
constexpr std::size_t findIf(std::string_view str, Predicate pred) noexcept
Find first character matching predicate.
constexpr bool endsWith(std::string_view str, std::string_view suffix) noexcept
Fast check if string ends with suffix.
std::string xmlEscape(std::string_view str, bool escapeNonAscii=false)
Escape string for use in XML/HTML content.
constexpr std::string_view trimStartIf(std::string_view str, Predicate pred) noexcept
Remove leading characters matching predicate.
std::string jsonEscape(std::string_view str, bool escapeNonAscii=false, bool escapeForHtml=false)
Escape string for use in JSON (RFC 8259).
bool decodeUtf8Codepoint(std::string_view str, std::size_t &i, uint32_t &codepoint) noexcept
Decode a UTF-8 sequence starting at position i.
std::string collapseWhitespace(std::string_view str)
Collapse consecutive whitespace characters to single space.
constexpr std::string_view commonSuffix(std::string_view lhs, std::string_view rhs) noexcept
Find longest common suffix of two strings.
std::string indent(std::string_view str, size_t spaces)
Add indentation to all lines.
constexpr std::string_view removeSuffix(std::string_view str, std::string_view suffix) noexcept
Remove suffix from string if present.
std::string urlEncode(std::string_view str)
Encode string for use in URLs (percent-encoding per RFC 3986).
std::size_t countOverlapping(std::string_view str, std::string_view substr) noexcept
Count overlapping occurrences of substring in string.
constexpr std::size_t findIfNot(std::string_view str, Predicate pred) noexcept
Find first character NOT matching predicate.
std::string jsonUnescape(std::string_view str) noexcept
Unescape JSON string literal (RFC 8259).
constexpr bool startsWith(std::string_view str, std::string_view prefix) noexcept
Fast check if string starts with prefix.
std::string xmlUnescape(std::string_view str) noexcept
Unescape XML/HTML entity references.
constexpr bool isIpv4Address(std::string_view str) noexcept
Validate IPv4 address format (RFC 791).
constexpr bool isDigit(char c) noexcept
Check if character is ASCII digit.
constexpr int compareIgnoreCase(std::string_view lhs, std::string_view rhs) noexcept
Case-insensitive three-way comparison.
constexpr bool naturalSort(std::string_view a, std::string_view b) noexcept
Case-sensitive natural sort comparison predicate.
std::string wordWrap(std::string_view str, size_t width)
Wrap text to specified width.
constexpr bool isIri(std::string_view str) noexcept
Validate IRI format (RFC 3987).
constexpr std::string_view trimEndIf(std::string_view str, Predicate pred) noexcept
Remove trailing characters matching predicate.
constexpr std::string_view substringBeforeLast(std::string_view str, std::string_view delimiter) noexcept
Extract substring before last occurrence of delimiter.
constexpr bool isDateTime(std::string_view str) noexcept
Validate RFC 3339 date-time format.
constexpr bool isWhitespace(char c) noexcept
Check if character is whitespace.
bool icontains(std::string_view str, std::string_view substr) noexcept
Case-insensitive check if string contains substring.
constexpr std::string_view substringBefore(std::string_view str, std::string_view delimiter) noexcept
Extract substring before first occurrence of delimiter.
constexpr bool isIpv6Address(std::string_view str) noexcept
Validate IPv6 address format (RFC 4291, RFC 5952).
constexpr bool isUriReference(std::string_view str) noexcept
Validate URI-reference format (RFC 3986).
constexpr bool isJsonPointer(std::string_view str) noexcept
Validate JSON Pointer format (RFC 6901).
constexpr bool contains(std::string_view str, std::string_view substr) noexcept
Fast check if string contains substring.
std::string dedent(std::string_view str)
Remove common leading whitespace from all lines.
constexpr int naturalCompare(std::string_view lhs, std::string_view rhs) noexcept
Natural sorting comparison (handles embedded numbers).
constexpr bool isAllDigits(std::string_view str) noexcept
Check if string contains only ASCII digits.
std::string removeWhitespace(std::string_view str)
Remove all whitespace characters from string.
constexpr bool isHostname(std::string_view str) noexcept
Validate hostname format (RFC 1123).
std::string padRight(std::string_view str, std::size_t width, char fillChar=' ')
Pad string on the right to reach specified width.
constexpr std::string_view trim(std::string_view str) noexcept
Remove leading and trailing whitespace from string.
constexpr bool isUriUnreserved(char c) noexcept
Check if character is URI unreserved (RFC 3986 Section 2.3).
constexpr bool isHexDigit(char c) noexcept
Check if character is ASCII hexadecimal digit.