nfx-stringutils 1.2.0
Cross-platform C++ string utilities library with validation and manipulation functions
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 <string>
36#include <string_view>
37
38namespace nfx::string
39{
40 //=====================================================================
41 // String utilities
42 //=====================================================================
43
44 //----------------------------------------------
45 // String validation
46 //----------------------------------------------
47
55 [[nodiscard]] inline constexpr bool hasExactLength( std::string_view str, std::size_t expectedLength ) noexcept;
56
63 [[nodiscard]] inline constexpr bool isEmpty( std::string_view str ) noexcept;
64
72 [[nodiscard]] inline constexpr bool isNullOrWhiteSpace( std::string_view str ) noexcept;
73
81 [[nodiscard]] inline constexpr bool isAllDigits( std::string_view str ) noexcept;
82
83 //----------------------------------------------
84 // Character classification
85 //----------------------------------------------
86
93 [[nodiscard]] inline constexpr bool isWhitespace( char c ) noexcept;
94
101 [[nodiscard]] inline constexpr bool isDigit( char c ) noexcept;
102
109 [[nodiscard]] inline constexpr bool isAlpha( char c ) noexcept;
110
117 [[nodiscard]] inline constexpr bool isAlphaNumeric( char c ) noexcept;
118
119 //----------------------------------------------
120 // String operations
121 //----------------------------------------------
122
130 [[nodiscard]] inline constexpr bool startsWith( std::string_view str, std::string_view prefix ) noexcept;
131
139 [[nodiscard]] inline constexpr bool endsWith( std::string_view str, std::string_view suffix ) noexcept;
140
148 [[nodiscard]] inline constexpr bool contains( std::string_view str, std::string_view substr ) noexcept;
149
157 [[nodiscard]] inline constexpr bool equals( std::string_view lhs, std::string_view rhs ) noexcept;
158
166 [[nodiscard]] inline bool iequals( std::string_view lhs, std::string_view rhs ) noexcept;
167
176 [[nodiscard]] inline std::size_t count( std::string_view str, std::string_view substr ) noexcept;
177
187 [[nodiscard]] inline std::size_t countOverlapping( std::string_view str, std::string_view substr ) noexcept;
188
196 [[nodiscard]] inline constexpr std::size_t count( std::string_view str, char ch ) noexcept;
197
207 [[nodiscard]] inline std::string replace( std::string_view str, std::string_view oldStr, std::string_view newStr );
208
218 [[nodiscard]] inline std::string replaceAll( std::string_view str, std::string_view oldStr, std::string_view newStr );
219
229 template <typename Container>
230 [[nodiscard]] inline std::string join( const Container& elements, std::string_view delimiter );
231
242 template <typename Iterator>
243 [[nodiscard]] inline std::string join( Iterator begin, Iterator end, std::string_view delimiter );
244
253 [[nodiscard]] inline std::string reverse( std::string_view str );
254
264 [[nodiscard]] inline constexpr std::size_t indexOf( std::string_view str, std::string_view substr ) noexcept;
265
275 [[nodiscard]] inline constexpr std::size_t lastIndexOf( std::string_view str, std::string_view substr ) noexcept;
276
277 //----------------------------------------------
278 // String formatting and padding
279 //----------------------------------------------
280
290 [[nodiscard]] inline std::string padLeft( std::string_view str, std::size_t width, char fillChar = ' ' );
291
301 [[nodiscard]] inline std::string padRight( std::string_view str, std::size_t width, char fillChar = ' ' );
302
313 [[nodiscard]] inline std::string center( std::string_view str, std::size_t width, char fillChar = ' ' );
314
323 [[nodiscard]] inline std::string repeat( std::string_view str, std::size_t count );
324
325 //----------------------------------------------
326 // String trimming
327 //----------------------------------------------
328
336 [[nodiscard]] inline constexpr std::string_view trimStart( std::string_view str ) noexcept;
337
345 [[nodiscard]] inline constexpr std::string_view trimEnd( std::string_view str ) noexcept;
346
354 [[nodiscard]] inline constexpr std::string_view trim( std::string_view str ) noexcept;
355
356 //----------------------------------------------
357 // String case conversion
358 //----------------------------------------------
359
368 [[nodiscard]] inline std::string toLower( std::string_view str );
369
378 [[nodiscard]] inline std::string toUpper( std::string_view str );
379
380 //----------------------------------------------
381 // Character case conversion
382 //----------------------------------------------
383
391 [[nodiscard]] inline constexpr char toLower( char c ) noexcept;
392
400 [[nodiscard]] inline constexpr char toUpper( char c ) noexcept;
401
402 //----------------------------------------------
403 // String parsing
404 //----------------------------------------------
405
414 [[nodiscard]] inline bool tryParseBool( std::string_view str, bool& result ) noexcept;
415
423 [[nodiscard]] inline bool tryParseInt( std::string_view str, int& result ) noexcept;
424
432 [[nodiscard]] inline bool tryParseUInt( std::string_view str, std::uint32_t& result ) noexcept;
433
441 [[nodiscard]] inline bool tryParseLong( std::string_view str, std::int64_t& result ) noexcept;
442
452 [[nodiscard]] inline bool tryParseFloat( std::string_view str, float& result ) noexcept;
453
463 [[nodiscard]] inline bool tryParseDouble( std::string_view str, double& result ) noexcept;
464
465 //----------------------------------------------
466 // Network and URI validation
467 //----------------------------------------------
468
469 //-----------------------------
470 // URI character classification
471 //-----------------------------
472
479 [[nodiscard]] inline constexpr bool isURIReserved( char c ) noexcept;
480
488 [[nodiscard]] inline constexpr bool isURIReserved( std::string_view str ) noexcept;
489
496 [[nodiscard]] inline constexpr bool isURIUnreserved( char c ) noexcept;
497
505 [[nodiscard]] inline constexpr bool isURIUnreserved( std::string_view str ) noexcept;
506
507 //-----------------------------
508 // IP address validation
509 //-----------------------------
510
519 [[nodiscard]] inline constexpr bool isIPv4Address( std::string_view str ) noexcept;
520
529 [[nodiscard]] inline constexpr bool isIPv6Address( std::string_view str ) noexcept;
530
531 //-----------------------------
532 // Host validation
533 //-----------------------------
534
544 [[nodiscard]] inline constexpr bool isValidHostname( std::string_view str ) noexcept;
545
554 [[nodiscard]] inline constexpr bool isDomainName( std::string_view str ) noexcept;
555
556 //-----------------------------
557 // Port validation
558 //-----------------------------
559
568 [[nodiscard]] inline constexpr bool isValidPort( std::string_view str ) noexcept;
569
581 template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
582 [[nodiscard, deprecated( "Will be removed in v2.0.0. Use manual range check: (port >= 0 && port <= 65535)" )]]
583 inline constexpr bool isValidPort( T port ) noexcept;
584
585 //-----------------------------
586 // Endpoint parsing
587 //-----------------------------
588
600 [[nodiscard]] inline bool tryParseEndpoint( std::string_view endpoint,
601 std::string_view& host,
602 uint16_t& port ) noexcept;
603} // namespace nfx::string
604
605#include "nfx/detail/string/Utils.inl"
constexpr bool isAlphaNumeric(char c) noexcept
Check if character is ASCII alphanumeric.
std::string toLower(std::string_view str)
Convert string to lowercase.
std::string replaceAll(std::string_view str, std::string_view oldStr, std::string_view newStr)
Replace all occurrences of substring with replacement.
bool tryParseDouble(std::string_view str, double &result) noexcept
Fast double parsing with error handling.
bool iequals(std::string_view lhs, std::string_view rhs) noexcept
Fast case-insensitive string comparison.
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 bool isNullOrWhiteSpace(std::string_view str) noexcept
Fast check if string is null, empty, or contains only whitespace.
bool tryParseFloat(std::string_view str, float &result) noexcept
Fast float parsing with error handling.
constexpr bool isEmpty(std::string_view str) noexcept
Fast check if string is empty.
std::string toUpper(std::string_view str)
Convert string to uppercase.
constexpr bool isValidPort(std::string_view str) noexcept
Validate port number string (RFC 6335).
bool tryParseEndpoint(std::string_view endpoint, std::string_view &host, uint16_t &port) noexcept
Parse network endpoint into host and port.
std::string repeat(std::string_view str, std::size_t count)
Repeat string specified number of times.
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.
constexpr std::string_view trimStart(std::string_view str) noexcept
Remove leading whitespace from string.
constexpr bool isURIReserved(char c) noexcept
Check if character is URI reserved (RFC 3986 Section 2.2).
bool tryParseBool(std::string_view str, bool &result) noexcept
Fast boolean parsing with error handling.
constexpr bool isValidHostname(std::string_view str) noexcept
Validate hostname format (RFC 1123).
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.
bool tryParseLong(std::string_view str, std::int64_t &result) noexcept
Fast long integer parsing with error handling.
constexpr bool isDomainName(std::string_view str) noexcept
Validate domain name format (RFC 1035).
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.
bool tryParseUInt(std::string_view str, std::uint32_t &result) noexcept
Fast unsigned integer parsing with error handling.
constexpr bool isIPv4Address(std::string_view str) noexcept
Validate IPv4 address format (RFC 791).
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 bool endsWith(std::string_view str, std::string_view suffix) noexcept
Fast check if string ends with suffix.
std::size_t countOverlapping(std::string_view str, std::string_view substr) noexcept
Count overlapping occurrences of substring in string.
bool tryParseInt(std::string_view str, int &result) noexcept
Fast integer parsing with error handling.
constexpr bool startsWith(std::string_view str, std::string_view prefix) noexcept
Fast check if string starts with prefix.
constexpr bool isDigit(char c) noexcept
Check if character is ASCII digit.
constexpr bool isURIUnreserved(char c) noexcept
Check if character is URI unreserved (RFC 3986 Section 2.3).
constexpr bool isWhitespace(char c) noexcept
Check if character is whitespace.
constexpr bool contains(std::string_view str, std::string_view substr) noexcept
Fast check if string contains substring.
constexpr bool isAllDigits(std::string_view str) noexcept
Check if string contains only ASCII digits.
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 isIPv6Address(std::string_view str) noexcept
Validate IPv6 address format (RFC 4291, RFC 5952).