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

Unified Diff: compiled/String.h

Issue 29548581: Issue 4128, 5138 - Add Parser and Serializer implemented in C++ Base URL: https://github.com/adblockplus/adblockpluscore.git
Patch Set: Created Sept. 18, 2017, 5:23 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/String.cpp » ('j') | compiled/String.cpp » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: compiled/String.h
diff --git a/compiled/String.h b/compiled/String.h
index a031621a087d2785a68ef2be6e1ec6918b08c6bb..043fdd075df5164eed9d52caad8135d42c46938e 100644
--- a/compiled/String.h
+++ b/compiled/String.h
@@ -21,6 +21,7 @@
#include <cstddef>
#include <cstring>
#include <type_traits>
+#include <utility>
#include "debug.h"
#include "library.h"
@@ -191,6 +192,38 @@ public:
}
}
}
+
+ int64_t toInt64() const
+ {
+ size_type len = length();
+ if (len == 0)
+ return 0;
Wladimir Palant 2017/12/21 10:30:36 What about using StringScanner so that we won't ne
+ size_type pos = 0;
+ bool negative = mBuf[0] == u'-';
+ if (negative)
+ {
+ ++pos;
+ }
+ int64_t result = 0;
+ for (; pos < len && mBuf[pos] >= u'0' && mBuf[pos] <= u'9'; ++pos)
+ {
+ result *= 10;
+ result += mBuf[pos] - u'0';
+ }
hub 2017/11/22 15:40:12 No overflow checking? int64_t is quite a lot of s
hub 2017/11/30 21:35:37 std::numeric_limits<int64_t>::digit10 is the value
+ return negative ? -result : result;
+ }
+
+ uint64_t toUInt64() const
Wladimir Palant 2017/12/21 10:30:36 Nit: This should be called toUint64().
+ {
+ uint64_t result = 0;
+ size_type len = length();
+ for (size_type pos = 0; pos < len && mBuf[pos] >= u'0' && mBuf[pos] <= u'9'; ++pos)
+ {
+ result *= 10;
+ result += mBuf[pos] - u'0';
+ }
+ return result;
+ }
Wladimir Palant 2017/12/21 10:30:36 General note: this should be ok as a first iterati
};
class DependentString : public String
@@ -405,3 +438,36 @@ public:
}
}
};
+
+template<typename Type>
+inline Type AdaptValue(const String& value);
hub 2017/11/22 15:40:12 I would have called this "lexical_cast<>" probably
Wladimir Palant 2017/12/21 10:30:36 Can't we have that as an instance method instead?
+
+template<>
+inline bool AdaptValue<bool>(const String& value)
+{
+ return value == u"true"_str;
+}
+template<>
+inline int AdaptValue<int>(const String& value)
+{
+ return static_cast<int>(value.toInt64());
+}
+template<>
+inline uint64_t AdaptValue<uint64_t>(const String& value)
+{
+ return value.toUInt64();
+}
+template<>
+inline OwnedString AdaptValue<OwnedString>(const String& value)
+{
+ return OwnedString{value};
+}
+
+DependentString TrimSpaces(const String& value);
+
+inline std::pair<DependentString, DependentString> SplitString(const String& value, String::size_type separatorPos)
+{
+ const auto secondBeginPos = separatorPos < String::npos ? separatorPos + 1 : String::npos;
+ return {DependentString{value, 0, separatorPos},
+ DependentString{value, secondBeginPos, value.length() - secondBeginPos}};
Wladimir Palant 2017/12/21 10:30:36 What is `value.length() - secondBeginPos` going to
sergei 2018/03/01 15:05:25 It's handled by the constructor of DependentString
+}
Wladimir Palant 2017/12/21 10:30:37 Why aren't these helpers methods of the String cla
sergei 2018/03/01 15:05:25 These functions are rather helper functions than m
« no previous file with comments | « no previous file | compiled/String.cpp » ('j') | compiled/String.cpp » ('J')

Powered by Google App Engine
This is Rietveld