util/StringStrip: add std::string_view overloads
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2009-2021 Max Kellermann <max.kellermann@gmail.com>
|
||||
* Copyright 2009-2022 Max Kellermann <max.kellermann@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "StringStrip.hxx"
|
||||
#include "CharUtil.hxx"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
const char *
|
||||
@@ -50,6 +51,18 @@ StripLeft(const char *p, const char *end) noexcept
|
||||
return p;
|
||||
}
|
||||
|
||||
std::string_view
|
||||
StripLeft(const std::string_view s) noexcept
|
||||
{
|
||||
auto i = std::find_if_not(s.begin(), s.end(),
|
||||
[](auto ch){ return IsWhitespaceOrNull(ch); });
|
||||
|
||||
return {
|
||||
i,
|
||||
s.end(),
|
||||
};
|
||||
}
|
||||
|
||||
const char *
|
||||
StripRight(const char *p, const char *end) noexcept
|
||||
{
|
||||
@@ -76,6 +89,15 @@ StripRight(char *p) noexcept
|
||||
p[new_length] = 0;
|
||||
}
|
||||
|
||||
std::string_view
|
||||
StripRight(std::string_view s) noexcept
|
||||
{
|
||||
auto i = std::find_if_not(s.rbegin(), s.rend(),
|
||||
[](auto ch){ return IsWhitespaceOrNull(ch); });
|
||||
|
||||
return s.substr(0, std::distance(i, s.rend()));
|
||||
}
|
||||
|
||||
char *
|
||||
Strip(char *p) noexcept
|
||||
{
|
||||
@@ -83,3 +105,9 @@ Strip(char *p) noexcept
|
||||
StripRight(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
std::string_view
|
||||
Strip(std::string_view s) noexcept
|
||||
{
|
||||
return StripRight(StripLeft(s));
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2009-2021 Max Kellermann <max.kellermann@gmail.com>
|
||||
* Copyright 2009-2022 Max Kellermann <max.kellermann@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -27,10 +27,10 @@
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef STRING_STRIP_HXX
|
||||
#define STRING_STRIP_HXX
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <string_view>
|
||||
|
||||
/**
|
||||
* Skips whitespace at the beginning of the string, and returns the
|
||||
@@ -57,6 +57,10 @@ StripLeft(char *p) noexcept
|
||||
const char *
|
||||
StripLeft(const char *p, const char *end) noexcept;
|
||||
|
||||
[[gnu::pure]]
|
||||
std::string_view
|
||||
StripLeft(std::string_view s) noexcept;
|
||||
|
||||
/**
|
||||
* Determine the string's end as if it was stripped on the right side.
|
||||
*/
|
||||
@@ -90,6 +94,10 @@ StripRight(const char *p, std::size_t length) noexcept;
|
||||
void
|
||||
StripRight(char *p) noexcept;
|
||||
|
||||
[[gnu::pure]]
|
||||
std::string_view
|
||||
StripRight(std::string_view s) noexcept;
|
||||
|
||||
/**
|
||||
* Skip whitespace at the beginning and terminate the string after the
|
||||
* last non-whitespace character.
|
||||
@@ -98,4 +106,6 @@ StripRight(char *p) noexcept;
|
||||
char *
|
||||
Strip(char *p) noexcept;
|
||||
|
||||
#endif
|
||||
[[gnu::pure]]
|
||||
std::string_view
|
||||
Strip(std::string_view s) noexcept;
|
||||
|
Reference in New Issue
Block a user