2019-06-05 11:09:42 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2020-03-13 19:10:43 +01:00
|
|
|
size_t
|
2020-08-02 09:36:56 +02:00
|
|
|
ircd::indexof(const string_view &in,
|
2020-03-15 01:20:12 +01:00
|
|
|
const string_views &tab)
|
2020-04-03 22:25:13 +02:00
|
|
|
noexcept
|
2020-03-13 19:10:43 +01:00
|
|
|
{
|
2020-08-02 09:36:56 +02:00
|
|
|
const auto it
|
2020-03-15 01:20:12 +01:00
|
|
|
{
|
2020-08-02 09:36:56 +02:00
|
|
|
std::find(tab.begin(), tab.end(), in)
|
|
|
|
};
|
2020-03-15 01:20:12 +01:00
|
|
|
|
2020-08-02 09:36:56 +02:00
|
|
|
const auto ret
|
2020-03-15 01:20:12 +01:00
|
|
|
{
|
2020-08-02 09:36:56 +02:00
|
|
|
std::distance(begin(tab), it)
|
|
|
|
};
|
2020-03-13 19:10:43 +01:00
|
|
|
|
2020-08-02 09:36:56 +02:00
|
|
|
assert(size_t(ret) <= tab.size());
|
|
|
|
return ret;
|
2020-03-13 19:10:43 +01:00
|
|
|
}
|
|
|
|
|
2019-08-03 06:02:40 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::toupper(const mutable_buffer &out,
|
|
|
|
const string_view &in)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
const auto stop
|
|
|
|
{
|
|
|
|
std::next(begin(in), std::min(size(in), size(out)))
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto end
|
|
|
|
{
|
|
|
|
std::transform(begin(in), stop, begin(out), []
|
2019-08-06 06:22:38 +02:00
|
|
|
(auto&& c)
|
2019-08-03 06:02:40 +02:00
|
|
|
{
|
|
|
|
return std::toupper(c);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-08-04 02:07:54 +02:00
|
|
|
assert(intptr_t(begin(out)) <= intptr_t(end));
|
2019-08-03 06:02:40 +02:00
|
|
|
return string_view
|
|
|
|
{
|
2019-08-04 02:07:54 +02:00
|
|
|
data(out), size_t(std::distance(begin(out), end))
|
2019-08-03 06:02:40 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-27 23:25:56 +02:00
|
|
|
#if defined(__SSE2__)
|
2019-08-06 06:22:38 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::tolower(const mutable_buffer &out,
|
|
|
|
const string_view &in)
|
|
|
|
noexcept
|
|
|
|
{
|
2019-08-07 06:41:14 +02:00
|
|
|
const auto stop
|
|
|
|
{
|
|
|
|
std::next(begin(in), std::min(size(in), size(out)))
|
|
|
|
};
|
|
|
|
|
2020-09-06 03:35:51 +02:00
|
|
|
auto *src_
|
2019-08-06 06:22:38 +02:00
|
|
|
{
|
2020-09-06 03:35:51 +02:00
|
|
|
reinterpret_cast<const __m128i_u *>(begin(in))
|
2019-08-06 06:22:38 +02:00
|
|
|
};
|
|
|
|
|
2020-09-06 03:35:51 +02:00
|
|
|
auto *dst
|
2019-08-06 06:22:38 +02:00
|
|
|
{
|
2020-09-06 03:35:51 +02:00
|
|
|
reinterpret_cast<__m128i_u *>(begin(out))
|
2019-08-06 06:22:38 +02:00
|
|
|
};
|
|
|
|
|
2020-09-06 03:35:51 +02:00
|
|
|
while(intptr_t(src_) < intptr_t(stop) - ssize_t(sizeof(m128u)))
|
2019-08-06 06:22:38 +02:00
|
|
|
{
|
2020-09-06 03:35:51 +02:00
|
|
|
const auto lit_A1 { _mm_set1_epi8('A' - 1) };
|
|
|
|
const auto lit_Z1 { _mm_set1_epi8('Z' + 1) };
|
|
|
|
const auto addend { _mm_set1_epi8('a' - 'A') };
|
|
|
|
const auto src { _mm_loadu_si128(src_++) };
|
|
|
|
const auto gte_A { _mm_cmpgt_epi8(src, lit_A1) };
|
|
|
|
const auto lte_Z { _mm_cmplt_epi8(src, lit_Z1) };
|
|
|
|
const auto mask { _mm_and_si128(gte_A, lte_Z) };
|
|
|
|
const auto ctrl_mask { _mm_and_si128(mask, addend) };
|
|
|
|
const auto result { _mm_add_epi8(src, ctrl_mask) };
|
|
|
|
_mm_storeu_si128(dst++, result);
|
2019-08-06 06:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto end{std::transform
|
|
|
|
(
|
|
|
|
reinterpret_cast<const char *>(src_),
|
|
|
|
stop,
|
|
|
|
reinterpret_cast<char *>(dst),
|
|
|
|
::tolower
|
|
|
|
)};
|
|
|
|
|
|
|
|
assert(intptr_t(begin(out)) <= intptr_t(end));
|
|
|
|
return string_view{begin(out), end};
|
|
|
|
}
|
|
|
|
#else
|
2019-08-03 06:02:40 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::tolower(const mutable_buffer &out,
|
|
|
|
const string_view &in)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
const auto stop
|
|
|
|
{
|
|
|
|
std::next(begin(in), std::min(size(in), size(out)))
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto end
|
|
|
|
{
|
2019-08-06 06:22:38 +02:00
|
|
|
std::transform(begin(in), stop, begin(out), ::tolower)
|
2019-08-03 06:02:40 +02:00
|
|
|
};
|
|
|
|
|
2019-08-04 02:07:54 +02:00
|
|
|
assert(intptr_t(begin(out)) <= intptr_t(end));
|
2019-08-03 06:02:40 +02:00
|
|
|
return string_view
|
|
|
|
{
|
2019-08-04 02:07:54 +02:00
|
|
|
data(out), size_t(std::distance(begin(out), end))
|
2019-08-03 06:02:40 +02:00
|
|
|
};
|
|
|
|
}
|
2019-08-06 06:22:38 +02:00
|
|
|
#endif
|
2019-08-03 06:02:40 +02:00
|
|
|
|
2019-06-05 11:09:42 +02:00
|
|
|
std::string
|
|
|
|
ircd::replace(const string_view &s,
|
2020-11-14 06:48:48 +01:00
|
|
|
const char before,
|
2019-06-05 11:09:42 +02:00
|
|
|
const string_view &after)
|
|
|
|
{
|
|
|
|
const uint32_t occurs
|
|
|
|
(
|
|
|
|
std::count(begin(s), end(s), before)
|
|
|
|
);
|
|
|
|
|
|
|
|
const size_t size
|
|
|
|
{
|
|
|
|
occurs? s.size() + (occurs * after.size()):
|
|
|
|
s.size() - occurs
|
|
|
|
};
|
|
|
|
|
|
|
|
return string(size, [&s, &before, &after]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
2020-11-14 06:48:48 +01:00
|
|
|
return replace(buf, s, before, after);
|
2019-06-05 11:09:42 +02:00
|
|
|
});
|
|
|
|
}
|
2020-11-13 11:14:27 +01:00
|
|
|
|
|
|
|
ircd::string_view
|
2020-11-14 06:48:48 +01:00
|
|
|
ircd::replace(const mutable_buffer &out_,
|
|
|
|
const string_view &in_,
|
|
|
|
const string_view &before,
|
|
|
|
const string_view &after)
|
2020-11-13 11:14:27 +01:00
|
|
|
{
|
2020-11-14 06:48:48 +01:00
|
|
|
string_view in(in_);
|
|
|
|
mutable_buffer out(out_);
|
|
|
|
size_t produced(0), consumed(0);
|
|
|
|
size_t p(in.find(before)); do
|
2020-11-13 11:14:27 +01:00
|
|
|
{
|
2020-11-14 06:48:48 +01:00
|
|
|
const string_view prologue
|
|
|
|
{
|
|
|
|
in.substr(0, p)
|
|
|
|
};
|
|
|
|
|
2021-03-18 23:31:14 +01:00
|
|
|
const auto prologue_copied
|
|
|
|
{
|
|
|
|
move(out, prologue)
|
|
|
|
};
|
|
|
|
|
|
|
|
consumed += consume(out, prologue_copied);
|
2020-11-14 06:48:48 +01:00
|
|
|
produced += size(prologue);
|
|
|
|
if(p != in.npos)
|
|
|
|
{
|
2021-03-18 23:31:14 +01:00
|
|
|
const auto after_copied
|
|
|
|
{
|
|
|
|
move(out, after)
|
|
|
|
};
|
|
|
|
|
|
|
|
consumed += consume(out, after_copied);
|
2020-11-14 06:48:48 +01:00
|
|
|
produced += size(after);
|
2022-07-15 02:21:38 +02:00
|
|
|
in = in.substr(p + size(before));
|
2020-11-14 06:48:48 +01:00
|
|
|
p = in.find(before);
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
while(1);
|
|
|
|
|
|
|
|
if(unlikely(produced > consumed))
|
|
|
|
throw std::out_of_range
|
|
|
|
{
|
|
|
|
"Insufficient buffer to replace string with string"
|
|
|
|
};
|
|
|
|
|
|
|
|
return string_view
|
|
|
|
{
|
2021-03-18 23:31:14 +01:00
|
|
|
data(out_), data(out)
|
2020-11-14 06:48:48 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::replace(const mutable_buffer &out_,
|
|
|
|
const string_view &in_,
|
|
|
|
const char before,
|
|
|
|
const string_view &after)
|
|
|
|
{
|
|
|
|
string_view in(in_);
|
|
|
|
mutable_buffer out(out_);
|
|
|
|
size_t produced(0), consumed(0);
|
|
|
|
size_t p(in.find(before)); do
|
|
|
|
{
|
|
|
|
const string_view prologue
|
|
|
|
{
|
|
|
|
in.substr(0, p)
|
|
|
|
};
|
|
|
|
|
2021-03-18 23:31:14 +01:00
|
|
|
const auto prologue_copied
|
|
|
|
{
|
|
|
|
move(out, prologue)
|
|
|
|
};
|
|
|
|
|
|
|
|
consumed += consume(out, prologue_copied);
|
2020-11-14 06:48:48 +01:00
|
|
|
produced += size(prologue);
|
|
|
|
if(p != in.npos)
|
|
|
|
{
|
2021-03-18 23:31:14 +01:00
|
|
|
const auto after_copied
|
|
|
|
{
|
|
|
|
move(out, after)
|
|
|
|
};
|
|
|
|
|
|
|
|
consumed += consume(out, after_copied);
|
2020-11-14 06:48:48 +01:00
|
|
|
produced += size(after);
|
|
|
|
in = in.substr(p + 1);
|
|
|
|
p = in.find(before);
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
while(1);
|
|
|
|
|
|
|
|
if(unlikely(produced > consumed))
|
|
|
|
throw std::out_of_range
|
|
|
|
{
|
|
|
|
"Insufficient buffer to replace character with string"
|
|
|
|
};
|
2020-11-13 11:14:27 +01:00
|
|
|
|
2020-11-14 06:48:48 +01:00
|
|
|
return string_view
|
|
|
|
{
|
2021-03-18 23:31:14 +01:00
|
|
|
data(out_), data(out)
|
2020-11-14 06:48:48 +01:00
|
|
|
};
|
|
|
|
}
|
2020-11-13 11:14:27 +01:00
|
|
|
|
2020-11-14 06:48:48 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::replace(const mutable_buffer &out,
|
|
|
|
const string_view &in,
|
|
|
|
const char before,
|
|
|
|
const char after)
|
|
|
|
noexcept
|
|
|
|
{
|
2020-11-13 11:14:27 +01:00
|
|
|
char *const __restrict__ outp
|
|
|
|
{
|
|
|
|
begin(out)
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *const __restrict__ inp
|
|
|
|
{
|
|
|
|
begin(in)
|
|
|
|
};
|
|
|
|
|
2020-11-14 06:48:48 +01:00
|
|
|
const size_t cpsz
|
|
|
|
{
|
|
|
|
std::min(size(out), size(in))
|
|
|
|
};
|
|
|
|
|
|
|
|
std::transform(inp, inp + cpsz, outp, [&before, &after]
|
|
|
|
(const char p) -> char
|
|
|
|
{
|
|
|
|
return 0
|
2020-12-18 13:02:27 +01:00
|
|
|
| (boolmask<char>(p == before) & after)
|
|
|
|
| (boolmask<char>(p != before) & p)
|
2020-11-14 06:48:48 +01:00
|
|
|
;
|
|
|
|
});
|
2020-11-13 11:14:27 +01:00
|
|
|
|
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
begin(out), cpsz
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::replace(const mutable_buffer &s,
|
2020-11-14 06:48:48 +01:00
|
|
|
const char before,
|
|
|
|
const char after)
|
2020-11-13 11:14:27 +01:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
char *const __restrict__ p
|
|
|
|
{
|
|
|
|
begin(s)
|
|
|
|
};
|
|
|
|
|
2020-11-14 06:48:48 +01:00
|
|
|
std::transform(p, p + size(s), p, [&before, &after]
|
|
|
|
(const char p) -> char
|
|
|
|
{
|
|
|
|
return 0
|
2020-12-18 13:02:27 +01:00
|
|
|
| (boolmask<char>(p == before) & after)
|
|
|
|
| (boolmask<char>(p != before) & p)
|
2020-11-14 06:48:48 +01:00
|
|
|
;
|
|
|
|
});
|
2020-11-13 11:14:27 +01:00
|
|
|
|
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
data(s), size(s)
|
|
|
|
};
|
|
|
|
}
|