summaryrefslogtreecommitdiff
path: root/third_party/bigint/BigIntegerUtils.cc
diff options
context:
space:
mode:
authorBo Xu <bo_xu@foxitsoftware.com>2014-12-02 13:06:22 -0800
committerBo Xu <bo_xu@foxitsoftware.com>2014-12-02 16:57:19 -0800
commit8ad0040bbb42d7738965eea99e029145cce0b24e (patch)
tree2ebd20da7529d0f0c58618fbd2883fdb1ae71142 /third_party/bigint/BigIntegerUtils.cc
parent610b751e894371a2933d7ba17efeee325381465d (diff)
downloadpdfium-8ad0040bbb42d7738965eea99e029145cce0b24e.tar.xz
Merge "Add big integer library""
This patch merges the 3 commits in master branch into one
Diffstat (limited to 'third_party/bigint/BigIntegerUtils.cc')
-rw-r--r--third_party/bigint/BigIntegerUtils.cc60
1 files changed, 60 insertions, 0 deletions
diff --git a/third_party/bigint/BigIntegerUtils.cc b/third_party/bigint/BigIntegerUtils.cc
new file mode 100644
index 0000000000..fac8ac34b1
--- /dev/null
+++ b/third_party/bigint/BigIntegerUtils.cc
@@ -0,0 +1,60 @@
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code by Matt McCutchen, see the LICENSE file.
+
+#include "BigIntegerUtils.hh"
+#include "BigUnsignedInABase.hh"
+
+std::string bigUnsignedToString(const BigUnsigned &x) {
+ return std::string(BigUnsignedInABase(x, 10));
+}
+
+std::string bigIntegerToString(const BigInteger &x) {
+ return (x.getSign() == BigInteger::negative)
+ ? (std::string("-") + bigUnsignedToString(x.getMagnitude()))
+ : (bigUnsignedToString(x.getMagnitude()));
+}
+
+BigUnsigned stringToBigUnsigned(const std::string &s) {
+ return BigUnsigned(BigUnsignedInABase(s, 10));
+}
+
+BigInteger stringToBigInteger(const std::string &s) {
+ // Recognize a sign followed by a BigUnsigned.
+ return (s[0] == '-') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1)), BigInteger::negative)
+ : (s[0] == '+') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1)))
+ : BigInteger(stringToBigUnsigned(s));
+}
+
+std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) {
+ BigUnsignedInABase::Base base;
+ long osFlags = os.flags();
+ if (osFlags & os.dec)
+ base = 10;
+ else if (osFlags & os.hex) {
+ base = 16;
+ if (osFlags & os.showbase)
+ os << "0x";
+ } else if (osFlags & os.oct) {
+ base = 8;
+ if (osFlags & os.showbase)
+ os << '0';
+ } else
+#ifdef FOXIT_CHROME_BUILD
+ abort();
+#else
+ throw "std::ostream << BigUnsigned: Could not determine the desired base from output-stream flags";
+#endif
+ std::string s = std::string(BigUnsignedInABase(x, base));
+ os << s;
+ return os;
+}
+
+std::ostream &operator <<(std::ostream &os, const BigInteger &x) {
+ if (x.getSign() == BigInteger::negative)
+ os << '-';
+ os << x.getMagnitude();
+ return os;
+}