StringUtil.cxx 2.19 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "StringUtil.hxx"
21
#include "CharUtil.hxx"
22
#include "ASCII.hxx"
23 24

#include <assert.h>
25
#include <string.h>
26

27
const char *
28
StripLeft(const char *p) noexcept
29
{
30
	while (IsWhitespaceNotNull(*p))
31 32 33 34 35
		++p;

	return p;
}

36
const char *
37
StripLeft(const char *p, const char *end) noexcept
38 39 40 41 42 43 44 45
{
	while (p < end && IsWhitespaceOrNull(*p))
		++p;

	return p;
}

const char *
46
StripRight(const char *p, const char *end) noexcept
47 48 49 50 51 52 53
{
	while (end > p && IsWhitespaceOrNull(end[-1]))
		--end;

	return end;
}

54
size_t
55
StripRight(const char *p, size_t length) noexcept
56
{
57
	while (length > 0 && IsWhitespaceOrNull(p[length - 1]))
58 59
		--length;

60 61 62 63
	return length;
}

void
64
StripRight(char *p) noexcept
65 66 67 68 69
{
	size_t old_length = strlen(p);
	size_t new_length = StripRight(p, old_length);
	p[new_length] = 0;
}
70

71
char *
72
Strip(char *p) noexcept
73 74 75
{
	p = StripLeft(p);
	StripRight(p);
76 77 78
	return p;
}

79
bool
80 81
StringArrayContainsCase(const char *const*haystack,
			const char *needle) noexcept
82
{
83 84
	assert(haystack != nullptr);
	assert(needle != nullptr);
85

86
	for (; *haystack != nullptr; ++haystack)
87
		if (StringEqualsCaseASCII(*haystack, needle))
88 89 90 91
			return true;

	return false;
}
92 93

void
94
ToUpperASCII(char *dest, const char *src, size_t size) noexcept
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
{
	assert(dest != nullptr);
	assert(src != nullptr);
	assert(size > 1);

	char *const end = dest + size - 1;

	do {
		char ch = *src++;
		if (ch == 0)
			break;

		*dest++ = ToUpperASCII(ch);
	} while (dest < end);

	*dest = 0;
}