🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

formatInt() doesn't properly handle uint64

Started by
0 comments, last by WitchLord 9 years, 3 months ago

formatInt() takes an int64 value, which means it doesn't properly handle uint64 values greater than INT64_MAX. Would it be possible to add something like the below to the script string?


+static string formatUInt(asQWORD value, const string &options, asUINT width)
+{
+    bool leftJustify = options.find("l") != string::npos;
+    bool padWithZero = options.find("0") != string::npos;
+    bool alwaysSign  = options.find("+") != string::npos;
+    bool spaceOnSign = options.find(" ") != string::npos;
+    bool hexSmall    = options.find("h") != string::npos;
+    bool hexLarge    = options.find("H") != string::npos;
+
+    string fmt = "%";
+    if( leftJustify ) fmt += "-";
+    if( alwaysSign ) fmt += "+";
+    if( spaceOnSign ) fmt += " ";
+    if( padWithZero ) fmt += "0";
+
+#ifdef _WIN32
+    fmt += "*I64u";
+#else
+#ifdef _LP64
+    fmt += "*l";
+#else
+    fmt += "*ll";
+#endif
+#endif
+
+    if( hexSmall ) fmt += "x";
+    else if( hexLarge ) fmt += "X";
+    else fmt += "u";
+
+    string buf;
+    buf.resize(width+30);
+#if _MSC_VER >= 1400 && !defined(__S3E__)
+    // MSVC 8.0 / 2005 or newer
+    sprintf_s(&buf[0], buf.size(), fmt.c_str(), width, value);
+#else
+    sprintf(&buf[0], fmt.c_str(), width, value);
+#endif
+    buf.resize(strlen(&buf[0]));
+
+    return buf;
+}

+    r = engine->RegisterGlobalFunction("string formatInt(uint64 val, const string &in options, uint width = 0)", asFUNCTION(formatUInt), asCALL_CDECL); assert(r >= 0);


Advertisement

I've included this in revision 2149.

Thanks,

Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement