]> granicus.if.org Git - taglib/commitdiff
Changed String::number() to use a standard function
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Sun, 19 May 2013 05:39:45 +0000 (14:39 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Sun, 19 May 2013 05:39:45 +0000 (14:39 +0900)
ConfigureChecks.cmake
config.h.cmake
taglib/toolkit/tstring.cpp

index 440a4414ec4e9cf4f41dcbd521d16256c6144f86..46611a0036385001135b7cb6bd6def9ff637685c 100644 (file)
@@ -183,6 +183,20 @@ if(NOT HAVE_GCC_BYTESWAP_16 OR NOT HAVE_GCC_BYTESWAP_32 OR NOT HAVE_GCC_BYTESWAP
   endif()
 endif()
 
+# Determine whether your compiler supports some safer version of sprintf.
+
+check_cxx_source_compiles("
+  #include <cstdio>
+  int main() { char buf[20]; snprintf(buf, 20, \"%d\", 1); return 0; }
+" HAVE_SNPRINTF)
+
+if(NOT HAVE_SNPRINTF)
+  check_cxx_source_compiles("
+    #include <cstdio>
+    int main() { char buf[20]; sprintf_s(buf, \"%d\", 1);  return 0; }
+  " HAVE_SPRINTF_S)
+endif()
+
 # Determine whether your compiler supports codecvt.
 
 check_cxx_source_compiles("
index 436ca440557c24e1793a7fd8ff5d5f22b6de5561..15d2f997ed1b28df2b6d07524b2c19b8968dd787 100644 (file)
 #cmakedefine   HAVE_WIN_ATOMIC 1
 #cmakedefine   HAVE_IA64_ATOMIC 1
 
+/* Defined if your compiler supports some safer version of sprintf */
+#cmakedefine   HAVE_SNPRINTF 1
+#cmakedefine   HAVE_SPRINTF_S 1
+
 /* Defined if you have libz */
 #cmakedefine   HAVE_ZLIB 1
 
index cf745f816e8cd5a8c884c9e43ddd9434ba392e5a..7b4d204059f6c45a32cd3d0cafedb0fe4ee78d8f 100644 (file)
@@ -33,7 +33,8 @@
 #include "trefcounter.h"
 
 #include <iostream>
-#include <string.h>
+#include <cstdio>
+#include <cstring>
 
 #if defined(HAVE_MSC_BYTESWAP)
 # include <stdlib.h>
@@ -597,31 +598,30 @@ bool String::isAscii() const
 
 String String::number(int n) // static
 {
-  if(n == 0)
-    return String("0");
+  static const size_t BufferSize = 11; // Sufficient to store "-214748364".
+  static const char *Format = "%d";
 
-  String charStack;
+  char buffer[BufferSize];
+  int length;
 
-  bool negative = n < 0;
+#if defined(HAVE_SNPRINTF)
 
-  if(negative)
-    n = n * -1;
+  length = snprintf(buffer, BufferSize, Format, n);
 
-  while(n > 0) {
-    int remainder = n % 10;
-    charStack += char(remainder + '0');
-    n = (n - remainder) / 10;
-  }
+#elif defined(HAVE_SPRINTF_S)
 
-  String s;
+  length = sprintf_s(buffer, Format, n);
 
-  if(negative)
-    s += '-';
+#else
 
-  for(int i = charStack.d->data.size() - 1; i >= 0; i--)
-    s += charStack.d->data[i];
+  length = sprintf(buffer, Format, n);
 
-  return s;
+#endif
+
+  if(length > 0)
+    return String(buffer);
+  else
+    return String::null;
 }
 
 TagLib::wchar &String::operator[](int i)