Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: compiled/String.h

Issue 29404594: Noissue - [emscripten] Replace sprintf() usage by safe alternatives (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Patch Set: Implemented a simple number conversion method Created April 6, 2017, 3:12 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | compiled/bindings.ipp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: compiled/String.h
===================================================================
--- a/compiled/String.h
+++ b/compiled/String.h
@@ -12,19 +12,20 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
+#include <algorithm>
#include <cstddef>
#include <cstring>
-#include <algorithm>
+#include <type_traits>
#include <emscripten.h>
#include "debug.h"
inline void String_assert_readonly(bool readOnly);
class String
@@ -352,9 +353,38 @@ public:
{
append(str.mBuf, str.length());
}
void append(value_type c)
{
append(&c, 1);
}
+
+ template<typename T,
+ typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
+ void append(T num)
+ {
+ bool negative = false;
+ if (num < 0)
+ {
+ negative = true;
+ num = -num;
+ }
+
+ size_type size = 0;
+ for (int i = num; i; i /= 10)
+ size++;
+ size = (size ? size : 1);
+
+ size_type pos = length();
+ grow((negative ? 1 : 0) + size);
+
+ if (negative)
+ mBuf[pos++] = '-';
+
+ for (int i = size - 1; i >= 0; i--)
+ {
+ mBuf[pos + i] = '0' + (num % 10);
+ num /= 10;
+ }
+ }
};
« no previous file with comments | « no previous file | compiled/bindings.ipp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld