StringView.hxx 3.69 KB
Newer Older
1
/*
2
 * Copyright (C) 2013-2017 Max Kellermann <max.kellermann@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef STRING_VIEW_HXX
#define STRING_VIEW_HXX

#include "ConstBuffer.hxx"

#include <string.h>

struct StringView : ConstBuffer<char> {
	StringView() = default;

40
	constexpr StringView(pointer_type _data, size_type _size) noexcept
41 42
		:ConstBuffer<char>(_data, _size) {}

43
	constexpr StringView(pointer_type _begin, pointer_type _end) noexcept
44 45
		:ConstBuffer<char>(_begin, _end - _begin) {}

46
	StringView(pointer_type _data) noexcept
47 48 49
		:ConstBuffer<char>(_data,
				   _data != nullptr ? strlen(_data) : 0) {}

50
	constexpr StringView(std::nullptr_t n) noexcept
51 52
		:ConstBuffer<char>(n) {}

53
	static constexpr StringView Empty() noexcept {
54 55 56
		return StringView("", size_t(0));
	}

57
	template<size_t n>
58
	static constexpr StringView Literal(const char (&_data)[n]) noexcept {
59 60 61 62
		static_assert(n > 0, "");
		return {_data, n - 1};
	}

63
	static constexpr StringView Literal() noexcept {
64 65 66
		return StringView("", size_t(0));
	}

67
	void SetEmpty() noexcept {
68 69 70 71 72
		data = "";
		size = 0;
	}

	gcc_pure
73
	pointer_type Find(char ch) const noexcept {
74 75 76
		return (pointer_type)memchr(data, ch, size);
	}

77
	StringView &operator=(std::nullptr_t) noexcept {
78 79 80 81 82
		data = nullptr;
		size = 0;
		return *this;
	}

83
	StringView &operator=(pointer_type _data) noexcept {
84 85 86 87 88 89
		data = _data;
		size = _data != nullptr ? strlen(_data) : 0;
		return *this;
	}

	gcc_pure
90
	bool StartsWith(StringView needle) const noexcept {
91 92 93 94
		return size >= needle.size &&
			memcmp(data, needle.data, needle.size) == 0;
	}

95 96 97 98 99 100 101
	gcc_pure
	bool EndsWith(StringView needle) const noexcept {
		return size >= needle.size &&
			memcmp(data + size - needle.size,
			       needle.data, needle.size) == 0;
	}

102
	gcc_pure
103
	bool Equals(StringView other) const noexcept {
104 105 106 107 108
		return size == other.size &&
			memcmp(data, other.data, size) == 0;
	}

	template<size_t n>
109
	bool EqualsLiteral(const char (&other)[n]) const noexcept {
110
		return Equals(Literal(other));
111 112 113
	}

	gcc_pure
114
	bool EqualsIgnoreCase(StringView other) const noexcept {
115 116 117 118 119
		return size == other.size &&
			strncasecmp(data, other.data, size) == 0;
	}

	template<size_t n>
120
	bool EqualsLiteralIgnoreCase(const char (&other)[n]) const noexcept {
121
		return EqualsIgnoreCase(Literal(other));
122
	}
123 124 125 126

	/**
	 * Skip all whitespace at the beginning.
	 */
127
	void StripLeft() noexcept;
128 129 130 131

	/**
	 * Skip all whitespace at the end.
	 */
132
	void StripRight() noexcept;
133

134
	void Strip() noexcept {
135 136 137
		StripLeft();
		StripRight();
	}
138 139 140
};

#endif