]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/string.qh
Reimplement ftos_mindecimals in a simpler and more correct way
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / string.qh
index ef1c00c62c3c5c0d2714327f8d649b16120fc5ef..384188091d0c4b0c92917bacafd3253707be3fe6 100644 (file)
@@ -1,5 +1,4 @@
-#ifndef STRING_H
-#define STRING_H
+#pragma once
 
 #include "nil.qh"
 #include "sort.qh"
@@ -17,7 +16,6 @@
        }
 #endif
 
-// TODO: macro
 string seconds_tostring(float sec)
 {
        float minutes = floor(sec / 60);
@@ -105,6 +103,9 @@ bool startsWithNocase(string haystack, string needle)
        return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0;
 }
 
+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 */
 string fstrunzone(string s)
 {
@@ -280,16 +281,24 @@ void buf_save(float buf, string pFilename)
 
 /**
  * converts a number to a string with the indicated number of decimals
- * works for up to 10 decimals!
  */
 string ftos_decimals(float number, int decimals)
 {
        // inhibit stupid negative zero
        if (number == 0) number = 0;
-       // we have sprintf...
        return sprintf("%.*f", decimals, number);
 }
 
+/**
+ * converts a number to a string with the minimum number of decimals
+ */
+string ftos_mindecimals(float number)
+{
+       // inhibit stupid negative zero
+       if (number == 0) number = 0;
+       return sprintf("%.7g", number);
+}
+
 int vercmp_recursive(string v1, string v2)
 {
        int dot1 = strstrofs(v1, ".", 0);
@@ -323,5 +332,3 @@ const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef";
 #define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0))
 #define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10)
 #define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS, (d), 1))
-
-#endif