centralize command formats; fix a few range bugs

This commit is contained in:
Martin Michelsen
2022-03-30 23:28:14 -07:00
parent db099ed2dd
commit 7dce8b6c2c
19 changed files with 2226 additions and 1937 deletions
+72 -5
View File
@@ -4,21 +4,56 @@
#include <stddef.h>
#include <string>
#include <phosg/Encoding.hh>
#include <phosg/Strings.hh>
int char16cmp(const char16_t* s1, const char16_t* s2, size_t count);
void char16cpy(char16_t* dest, const char16_t* src, size_t count);
#define countof(F) (sizeof(F) / sizeof(F[0]))
int char16ncmp(const char16_t* s1, const char16_t* s2, size_t count);
void char16ncpy(char16_t* dest, const char16_t* src, size_t count);
size_t char16len(const char16_t* s);
void encode_sjis(char* dest, const char16_t* source, size_t dest_count);
void decode_sjis(char16_t* dest, const char* source, size_t dest_count);
std::string encode_sjis(const char16_t* source);
std::u16string decode_sjis(const char* source);
std::string encode_sjis(const char16_t* source, size_t src_count);
std::u16string decode_sjis(const char* source, size_t src_count);
std::string encode_sjis(const std::u16string& source);
std::u16string decode_sjis(const std::string& source);
template <typename DestT, typename SrcT = DestT>
void strncpy_t(DestT*, const SrcT*, size_t) {
static_assert(always_false<DestT, SrcT>::v,
"unspecialized strcpy_t should never be called");
}
template <>
inline void strncpy_t<char>(char* dest, const char* src, size_t count) {
strncpy(dest, src, count);
}
template <>
inline void strncpy_t<char, char16_t>(char* dest, const char16_t* src, size_t count) {
encode_sjis(dest, src, count);
}
template <>
inline void strncpy_t<char16_t, char>(char16_t* dest, const char* src, size_t count) {
decode_sjis(dest, src, count);
}
template <>
inline void strncpy_t<char16_t>(char16_t* dest, const char16_t* src, size_t count) {
char16ncpy(dest, src, count);
}
void add_language_marker_inplace(char* s, char marker, size_t dest_count);
void add_language_marker_inplace(char16_t* s, char16_t marker, size_t dest_count);
void remove_language_marker_inplace(char* s);
@@ -29,6 +64,7 @@ std::string remove_language_marker(const std::string& s);
std::u16string remove_language_marker(const std::u16string& s);
template <typename T>
void replace_char_inplace(T* a, T f, T r) {
while (*a) {
@@ -58,6 +94,8 @@ size_t add_color_inplace(T* a, size_t max_chars) {
*(d++) = '%';
} else if (*a == 'n') {
*(d++) = '#';
} else if (*a == '\0') {
break;
} else {
*(d++) = *a;
}
@@ -70,3 +108,32 @@ size_t add_color_inplace(T* a, size_t max_chars) {
return d - orig_d;
}
template <typename T>
void add_color(StringWriter& w, const T* src, size_t max_input_chars = SIZE_T_MAX) {
for (size_t x = 0; (x < max_input_chars) && *src; x++) {
if (*src == '$') {
w.put<T>('\t');
} else if (*src == '#') {
w.put<T>('\n');
} else if (*src == '%') {
src++;
x++;
if (*src == 's') {
w.put<T>('$');
} else if (*src == '%') {
w.put<T>('%');
} else if (*src == 'n') {
w.put<T>('#');
} else if (*src == '\0') {
break;
} else {
w.put<T>(*src);
}
} else {
w.put<T>(*src);
}
src++;
}
w.put<T>(0);
}