2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2013-12-14 12:58:26 +01:00
|
|
|
|
2014-12-03 21:38:06 +01:00
|
|
|
#include "DivideString.hxx"
|
2017-07-05 17:20:02 +02:00
|
|
|
#include "StringStrip.hxx"
|
2013-12-14 12:58:26 +01:00
|
|
|
|
2020-05-01 04:25:55 +02:00
|
|
|
#include <cstring>
|
2013-12-14 12:58:26 +01:00
|
|
|
|
2017-05-16 10:22:36 +02:00
|
|
|
DivideString::DivideString(const char *s, char separator, bool strip) noexcept
|
2013-12-14 12:58:26 +01:00
|
|
|
:first(nullptr)
|
|
|
|
{
|
2020-05-01 04:25:55 +02:00
|
|
|
const char *x = std::strchr(s, separator);
|
2013-12-14 12:58:26 +01:00
|
|
|
if (x == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t length = x - s;
|
|
|
|
second = x + 1;
|
|
|
|
|
2014-12-04 23:05:44 +01:00
|
|
|
if (strip)
|
|
|
|
second = StripLeft(second);
|
|
|
|
|
|
|
|
if (strip) {
|
|
|
|
const char *end = s + length;
|
|
|
|
s = StripLeft(s);
|
|
|
|
end = StripRight(s, end);
|
|
|
|
length = end - s;
|
|
|
|
}
|
|
|
|
|
2013-12-14 12:58:26 +01:00
|
|
|
first = new char[length + 1];
|
|
|
|
memcpy(first, s, length);
|
|
|
|
first[length] = 0;
|
|
|
|
}
|