X-Git-Url: https://de.git.xonotic.org/?p=xonotic%2Fxonotic-data.pk3dir.git;a=blobdiff_plain;f=qcsrc%2Flib%2Fstring.qh;h=c4b594d40fd846cf81b6e5e20de920abbb110642;hp=9f0c5ca13bcaea342e6e27537931dc7b076aeff4;hb=45d8904a100765555e622598a39967963733df1d;hpb=d5a954d00e5c66bd5e68eadef172e50cf392bc4c diff --git a/qcsrc/lib/string.qh b/qcsrc/lib/string.qh index 9f0c5ca13..c4b594d40 100644 --- a/qcsrc/lib/string.qh +++ b/qcsrc/lib/string.qh @@ -4,6 +4,25 @@ #include "sort.qh" #include "oo.qh" +// string logic +// +// true: is truthy +// == "": is equal to "" +// is "": has the same string index as the string constant "" +// strunzone: can be strunzoned +// +// | | true | == "" | is "" | strunzone | +// | :----------: | :--: | :---: | :---: | :-------: | +// | nil | | yes | | | +// | strcat(nil) | yes | yes | | | +// | strzone(nil) | yes | yes | | yes | +// | "" | yes | yes | yes | | +// | strcat("") | yes | yes | | | +// | strzone("") | yes | yes | | yes | +// | "s" | yes | | | | +// | strcat("s") | yes | | | | +// | strzone("s") | yes | | | yes | + #ifdef CSQC float stringwidth_colors(string s, vector theSize) { @@ -27,6 +46,21 @@ } #endif +#define strcpy(this, s) MACRO_BEGIN \ + if (this) { \ + strunzone(this); \ + } \ + this = strzone(s); \ +MACRO_END + +#define strfree(this) MACRO_BEGIN \ + if (this) { \ + strunzone(this); \ + } \ + this = string_null; \ +MACRO_END + +ERASEABLE string seconds_tostring(float sec) { float minutes = floor(sec / 60); @@ -34,6 +68,7 @@ string seconds_tostring(float sec) return sprintf("%d:%02d", minutes, sec); } +ERASEABLE string format_time(float seconds) { seconds = floor(seconds + 0.5); @@ -47,6 +82,7 @@ string format_time(float seconds) else return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds); } +ERASEABLE string mmsss(float tenths) { tenths = floor(tenths + 0.5); @@ -56,6 +92,7 @@ string mmsss(float tenths) return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1)); } +ERASEABLE string mmssss(float hundredths) { hundredths = floor(hundredths + 0.5); @@ -67,6 +104,7 @@ string mmssss(float hundredths) int ColorTranslateMode; +ERASEABLE string ColorTranslateRGB(string s) { return (ColorTranslateMode & 1) ? strdecolorize(s) : s; @@ -111,6 +149,7 @@ string CCR(string input) #define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0) +ERASEABLE bool startsWithNocase(string haystack, string needle) { return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0; @@ -120,6 +159,7 @@ noref string _endsWith_suffix; #define endsWith(this, suffix) (_endsWith_suffix = suffix, substring(this, -strlen(_endsWith_suffix), -1) == _endsWith_suffix) /** unzone the string, and return it as tempstring. Safe to be called on string_null */ +ERASEABLE string fstrunzone(string s) { if (!s) return s; @@ -129,6 +169,7 @@ string fstrunzone(string s) } /** returns first word */ +ERASEABLE string car(string s) { int o = strstrofs(s, " ", 0); @@ -137,6 +178,7 @@ string car(string s) } /** returns all but first word */ +ERASEABLE string cdr(string s) { int o = strstrofs(s, " ", 0); @@ -144,6 +186,7 @@ string cdr(string s) return substring(s, o + 1, strlen(s) - (o + 1)); } +ERASEABLE string cons(string a, string b) { if (a == "") return b; @@ -151,11 +194,21 @@ string cons(string a, string b) return strcat(a, " ", b); } +ERASEABLE +string cons_mid(string a, string mid, string b) +{ + if (a == "") return b; + if (b == "") return a; + return strcat(a, mid, b); +} + +ERASEABLE string substring_range(string s, float b, float e) { return substring(s, b, e - b); } +ERASEABLE string swapwords(string str, float i, float j) { float n; @@ -177,10 +230,13 @@ string swapwords(string str, float i, float j) } string _shufflewords_str; +ERASEABLE void _shufflewords_swapfunc(float i, float j, entity pass) { _shufflewords_str = swapwords(_shufflewords_str, i, j); } + +ERASEABLE string shufflewords(string str) { _shufflewords_str = str; @@ -191,6 +247,7 @@ string shufflewords(string str) return str; } +ERASEABLE string unescape(string in) { in = strzone(in); // but it doesn't seem to be necessary in my tests at least @@ -215,6 +272,7 @@ string unescape(string in) return str; } +ERASEABLE string strwords(string s, int w) { int endpos = 0; @@ -226,6 +284,7 @@ string strwords(string s, int w) #define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0) +ERASEABLE int u8_strsize(string s) { int l = 0; @@ -238,6 +297,8 @@ int u8_strsize(string s) return l; } +// List of Unicode spaces: http://jkorpela.fi/chars/spaces.html +ERASEABLE bool isInvisibleString(string s) { s = strdecolorize(s); @@ -253,8 +314,26 @@ bool isInvisibleString(string s) case 192: // charmap space if (!utf8) break; return false; - case 160: // space in unicode fonts case 0xE000 + 192: // utf8 charmap space + case 0x00A0: // NO-BREAK SPACE + //case 0x1680: // OGHAM SPACE MARK + case 0x180E: // MONGOLIAN VOWEL SEPARATOR + case 0x2000: // EN QUAD + case 0x2001: // EM QUAD + case 0x2002: // EN SPACE + case 0x2003: // EM SPACE + case 0x2004: // THREE-PER-EM SPACE + case 0x2005: // FOUR-PER-EM SPACE + case 0x2006: // SIX-PER-EM SPACE + case 0x2007: // FIGURE SPACE + case 0x2008: // PUNCTUATION SPACE + case 0x2009: // THIN SPACE + case 0x200A: // HAIR SPACE + case 0x200B: // ZERO WIDTH SPACE + case 0x202F: // NARROW NO-BREAK SPACE + case 0x205F: // MEDIUM MATHEMATICAL SPACE + case 0x3000: // IDEOGRAPHIC SPACE + case 0xFEFF: // ZERO WIDTH NO-BREAK SPACE if (utf8) break; default: return false; @@ -265,6 +344,7 @@ bool isInvisibleString(string s) // Multiline text file buffers +ERASEABLE int buf_load(string pFilename) { int buf = buf_create(); @@ -282,6 +362,7 @@ int buf_load(string pFilename) return buf; } +ERASEABLE void buf_save(float buf, string pFilename) { int fh = fopen(pFilename, FILE_WRITE); @@ -295,6 +376,7 @@ void buf_save(float buf, string pFilename) /** * converts a number to a string with the indicated number of decimals */ +ERASEABLE string ftos_decimals(float number, int decimals) { // inhibit stupid negative zero @@ -305,6 +387,7 @@ string ftos_decimals(float number, int decimals) /** * converts a number to a string with the minimum number of decimals */ +ERASEABLE string ftos_mindecimals(float number) { // inhibit stupid negative zero @@ -312,6 +395,7 @@ string ftos_mindecimals(float number) return sprintf("%.7g", number); } +ERASEABLE int vercmp_recursive(string v1, string v2) { int dot1 = strstrofs(v1, ".", 0); @@ -330,6 +414,7 @@ int vercmp_recursive(string v1, string v2) else return (dot2 == -1) ? 1 : vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999)); } +ERASEABLE int vercmp(string v1, string v2) { if (strcasecmp(v1, v2) == 0) return 0; // early out check