]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/string.qh
Use checkColorCode in textLengthUpToWidth and get rid of skipIncompleteTag
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / string.qh
index 9f0c5ca13bcaea342e6e27537931dc7b076aeff4..7f3443d6c04fc5cdb54b33a37498366ee4438d0e 100644 (file)
@@ -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)
        {
        }
 #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;
@@ -89,8 +127,6 @@ string autocvar_hud_colorset_background = "7";   // BG - White // neutral/unimpo
 /** color code replace, place inside of sprintf and parse the string */
 string CCR(string input)
 {
-       // See the autocvar declarations in util.qh for default values
-
        // foreground/normal colors
        input = strreplace("^F1", strcat("^", autocvar_hud_colorset_foreground_1), input);
        input = strreplace("^F2", strcat("^", autocvar_hud_colorset_foreground_2), input);
@@ -111,6 +147,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 +157,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 +167,7 @@ string fstrunzone(string s)
 }
 
 /** returns first word */
+ERASEABLE
 string car(string s)
 {
        int o = strstrofs(s, " ", 0);
@@ -137,6 +176,7 @@ string car(string s)
 }
 
 /** returns all but first word */
+ERASEABLE
 string cdr(string s)
 {
        int o = strstrofs(s, " ", 0);
@@ -144,6 +184,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 +192,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 +228,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 +245,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 +270,7 @@ string unescape(string in)
        return str;
 }
 
+ERASEABLE
 string strwords(string s, int w)
 {
        int endpos = 0;
@@ -226,6 +282,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 +295,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 +312,29 @@ 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 0xE000: // invisible char of the utf8 quake charmap
+                       case 0xE00A: // invisible char of the utf8 quake charmap
+                       case 0xE0A0: // invisible char of the utf8 quake charmap
+                       case 0xE020: // invisible char of the utf8 quake charmap
+                       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 +345,7 @@ bool isInvisibleString(string s)
 
 // Multiline text file buffers
 
+ERASEABLE
 int buf_load(string pFilename)
 {
        int buf = buf_create();
@@ -282,6 +363,7 @@ int buf_load(string pFilename)
        return buf;
 }
 
+ERASEABLE
 void buf_save(float buf, string pFilename)
 {
        int fh = fopen(pFilename, FILE_WRITE);
@@ -295,6 +377,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 +388,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 +396,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 +415,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
@@ -350,3 +436,67 @@ const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
 
 const string DIGITS = "0123456789";
 #define IS_DIGIT(d) (strstrofs(DIGITS, (d), 0) >= 0)
+
+// returns true if the caret at position pos is escaped
+ERASEABLE
+bool isCaretEscaped(string theText, float pos)
+{
+       // count all the previous carets
+       int carets = 0;
+       while(pos - carets >= 1 && substring(theText, pos - carets - 1, 1) == "^")
+               ++carets;
+       // if number of previous carets is odd then this carets is escaped
+       return (carets & 1);
+}
+
+ERASEABLE
+bool isValidColorCodeValue(string theText, int cc_len, int tag_start)
+{
+       if (cc_len == 2)
+               return IS_DIGIT(substring(theText, tag_start + 1, 1));
+       if (cc_len == 5)
+               return (IS_HEXDIGIT(substring(theText, tag_start + 2, 1))
+                       && IS_HEXDIGIT(substring(theText, tag_start + 3, 1))
+                       && IS_HEXDIGIT(substring(theText, tag_start + 4, 1)));
+       return false;
+}
+
+// it returns 0 if pos is NOT in the middle or at the end of a color code
+// otherwise it returns a vector with color code length as the first component
+// and the offset from '^' position to pos as the second component
+// e.g.:
+// "j^2kl" | returns 0 if pos == 0 or 1 or 4
+//    ^^   | returns '2 1' or '2 2' if pos == 2 or 3
+ERASEABLE
+vector checkColorCode(string theText, int text_len, int pos, bool check_at_the_end)
+{
+       if (text_len == 0)
+               text_len = strlen(theText);
+       string tag_type = "^";
+       int cc_len = 2;
+       int tag_len = 1;
+
+       LABEL(check_color_tag)
+
+       int ofs = cc_len;
+       if (!check_at_the_end)
+               ofs--;
+       for (; ofs >= 1; ofs--)
+       {
+               if (!(pos >= ofs && text_len >= pos + (cc_len - ofs)))
+                       continue;
+               if(substring(theText, pos - ofs, tag_len) == tag_type)
+               {
+                       if (!isCaretEscaped(theText, pos - ofs) && isValidColorCodeValue(theText, cc_len, pos - ofs))
+                               return eX * cc_len + eY * ofs;
+               }
+       }
+       if (cc_len == 2)
+       {
+               tag_type = "^x";
+               cc_len = 5;
+               tag_len = 2;
+               goto check_color_tag;
+       }
+       return '0 0 0';
+}