/*
* Determine if string "t" is equal to string "t"
*/
-bool streq(concptr a, concptr b)
+bool streq(std::string_view a, std::string_view b)
{
- return (!strcmp(a, b));
+ return a == b;
}
-
/*
* Determine if string "t" is a suffix of string "s"
*/
-bool suffix(concptr s, concptr t)
+bool suffix(std::string_view s, std::string_view t)
{
- int tlen = strlen(t);
- int slen = strlen(s);
-
- /* Check for incompatible lengths */
- if (tlen > slen) return false;
+ //! @todo C++20 では ends_with が使用可能
+ if (t.size() > s.size()) {
+ return false;
+ }
- /* Compare "t" to the end of "s" */
- return (!strcmp(s + slen - tlen, t));
+ return s.compare(s.size() - t.size(), s.npos, t) == 0;
}
-
/*
* Determine if string "t" is a prefix of string "s"
*/
-bool prefix(concptr s, concptr t)
+bool prefix(std::string_view s, std::string_view t)
{
- /* Scan "t" */
- while (*t)
- {
- /* Compare content and length */
- if (*t++ != *s++) return false;
- }
-
- /* Matched, we have a prefix */
- return true;
+ //! @todo C++20 では starts_with が使用可能
+ return s.substr(0, t.size()) == t;
}
#include "system/h-basic.h"
+#include <string_view>
/*
* Extremely basic stuff, like global temp and constant variables.
/**** Available Functions ****/
/* Test equality, prefix, suffix */
-extern bool streq(concptr s, concptr t);
-extern bool prefix(concptr s, concptr t);
-extern bool suffix(concptr s, concptr t);
-
+extern bool streq(std::string_view s, std::string_view t);
+extern bool prefix(std::string_view s, std::string_view t);
+extern bool suffix(std::string_view s, std::string_view t);
/* Print an error message */
extern void plog(concptr str);
extern int count_bits(BIT_FLAGS x);
extern int mysqrt(int n);
-
* parsing "\xFF" into a (signed) char. Whoever thought of making
* the "sign" of a "char" undefined is a complete moron. Oh well.
*/
-void text_to_ascii(char *buf, concptr str)
+void text_to_ascii(char *buf, std::string_view sv)
{
char *s = buf;
+ auto str = sv.data();
while (*str) {
if (*str == '\\') {
str++;
/*
* Hack -- convert a string into a printable form
*/
-void ascii_to_text(char *buf, concptr str)
+void ascii_to_text(char *buf, std::string_view sv)
{
char *s = buf;
+ auto str = sv.data();
while (*str) {
byte i = (byte)(*str++);
if (i == 31) {
extern concptr macro_trigger_name[MAX_MACRO_TRIG];
extern concptr macro_trigger_keycode[2][MAX_MACRO_TRIG];
-void text_to_ascii(char *buf, concptr str);
-void ascii_to_text(char *buf, concptr str);
+void text_to_ascii(char *buf, std::string_view sv);
+void ascii_to_text(char *buf, std::string_view sv);
size_t angband_strcpy(char *buf, concptr src, size_t bufsize);
size_t angband_strcat(char *buf, concptr src, size_t bufsize);
char *angband_strstr(concptr haystack, concptr needle);