util/StringStrip: add std::string_view overloads
This commit is contained in:
parent
10197a0041
commit
b806b0a97f
|
@ -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
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
@ -30,6 +30,7 @@
|
||||||
#include "StringStrip.hxx"
|
#include "StringStrip.hxx"
|
||||||
#include "CharUtil.hxx"
|
#include "CharUtil.hxx"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
|
@ -50,6 +51,18 @@ StripLeft(const char *p, const char *end) noexcept
|
||||||
return p;
|
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 *
|
const char *
|
||||||
StripRight(const char *p, const char *end) noexcept
|
StripRight(const char *p, const char *end) noexcept
|
||||||
{
|
{
|
||||||
|
@ -76,6 +89,15 @@ StripRight(char *p) noexcept
|
||||||
p[new_length] = 0;
|
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 *
|
char *
|
||||||
Strip(char *p) noexcept
|
Strip(char *p) noexcept
|
||||||
{
|
{
|
||||||
|
@ -83,3 +105,9 @@ Strip(char *p) noexcept
|
||||||
StripRight(p);
|
StripRight(p);
|
||||||
return 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
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
@ -27,10 +27,10 @@
|
||||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef STRING_STRIP_HXX
|
#pragma once
|
||||||
#define STRING_STRIP_HXX
|
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Skips whitespace at the beginning of the string, and returns the
|
* Skips whitespace at the beginning of the string, and returns the
|
||||||
|
@ -57,6 +57,10 @@ StripLeft(char *p) noexcept
|
||||||
const char *
|
const char *
|
||||||
StripLeft(const char *p, const char *end) noexcept;
|
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.
|
* 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
|
void
|
||||||
StripRight(char *p) noexcept;
|
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
|
* Skip whitespace at the beginning and terminate the string after the
|
||||||
* last non-whitespace character.
|
* last non-whitespace character.
|
||||||
|
@ -98,4 +106,6 @@ StripRight(char *p) noexcept;
|
||||||
char *
|
char *
|
||||||
Strip(char *p) noexcept;
|
Strip(char *p) noexcept;
|
||||||
|
|
||||||
#endif
|
[[gnu::pure]]
|
||||||
|
std::string_view
|
||||||
|
Strip(std::string_view s) noexcept;
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
/*
|
||||||
|
* Unit tests for src/util/
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "util/StringStrip.hxx"
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
using std::string_view_literals::operator""sv;
|
||||||
|
|
||||||
|
TEST(StringStrip, StripLeft)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(StripLeft(""sv), ""sv);
|
||||||
|
EXPECT_EQ(StripLeft(" "sv), ""sv);
|
||||||
|
EXPECT_EQ(StripLeft("\t"sv), ""sv);
|
||||||
|
EXPECT_EQ(StripLeft("\0"sv), ""sv);
|
||||||
|
EXPECT_EQ(StripLeft(" a "sv), "a "sv);
|
||||||
|
EXPECT_EQ(StripLeft("\0a\0"sv), "a\0"sv);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(StringStrip, StripRight)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(StripRight(""sv), ""sv);
|
||||||
|
EXPECT_EQ(StripRight(" "sv), ""sv);
|
||||||
|
EXPECT_EQ(StripRight("\t"sv), ""sv);
|
||||||
|
EXPECT_EQ(StripRight("\0"sv), ""sv);
|
||||||
|
EXPECT_EQ(StripRight(" a "sv), " a"sv);
|
||||||
|
EXPECT_EQ(StripRight("\0a\0"sv), "\0a"sv);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(StringStrip, Strip)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(Strip(""sv), ""sv);
|
||||||
|
EXPECT_EQ(Strip(" "sv), ""sv);
|
||||||
|
EXPECT_EQ(Strip("\t"sv), ""sv);
|
||||||
|
EXPECT_EQ(Strip("\0"sv), ""sv);
|
||||||
|
EXPECT_EQ(Strip(" a "sv), "a"sv);
|
||||||
|
EXPECT_EQ(Strip("\0a\0"sv), "a"sv);
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ test(
|
||||||
'TestIntrusiveList.cxx',
|
'TestIntrusiveList.cxx',
|
||||||
'TestMimeType.cxx',
|
'TestMimeType.cxx',
|
||||||
'TestSplitString.cxx',
|
'TestSplitString.cxx',
|
||||||
|
'TestStringStrip.cxx',
|
||||||
'TestTemplateString.cxx',
|
'TestTemplateString.cxx',
|
||||||
'TestUriExtract.cxx',
|
'TestUriExtract.cxx',
|
||||||
'TestUriQueryParser.cxx',
|
'TestUriQueryParser.cxx',
|
||||||
|
|
Loading…
Reference in New Issue