Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: third_party/bigint/BigIntegerUtils.cc

Issue 773443004: Initial check in of big integer library (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/bigint/BigIntegerLibrary.hh ('k') | third_party/bigint/BigIntegerUtils.hh » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "BigIntegerUtils.hh"
2 #include "BigUnsignedInABase.hh"
3
4 std::string bigUnsignedToString(const BigUnsigned &x) {
5 return std::string(BigUnsignedInABase(x, 10));
6 }
7
8 std::string bigIntegerToString(const BigInteger &x) {
9 return (x.getSign() == BigInteger::negative)
10 ? (std::string("-") + bigUnsignedToString(x.getMagnitude()))
11 : (bigUnsignedToString(x.getMagnitude()));
12 }
13
14 BigUnsigned stringToBigUnsigned(const std::string &s) {
15 return BigUnsigned(BigUnsignedInABase(s, 10));
16 }
17
18 BigInteger stringToBigInteger(const std::string &s) {
19 // Recognize a sign followed by a BigUnsigned.
20 return (s[0] == '-') ? BigInteger(stringToBigUnsigned(s.substr(1, s.leng th() - 1)), BigInteger::negative)
21 : (s[0] == '+') ? BigInteger(stringToBigUnsigned(s.substr(1, s.l ength() - 1)))
22 : BigInteger(stringToBigUnsigned(s));
23 }
24
25 std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) {
26 BigUnsignedInABase::Base base;
27 long osFlags = os.flags();
28 if (osFlags & os.dec)
29 base = 10;
30 else if (osFlags & os.hex) {
31 base = 16;
32 if (osFlags & os.showbase)
33 os << "0x";
34 } else if (osFlags & os.oct) {
35 base = 8;
36 if (osFlags & os.showbase)
37 os << '0';
38 } else
39 throw "std::ostream << BigUnsigned: Could not determine the desi red base from output-stream flags";
40 std::string s = std::string(BigUnsignedInABase(x, base));
41 os << s;
42 return os;
43 }
44
45 std::ostream &operator <<(std::ostream &os, const BigInteger &x) {
46 if (x.getSign() == BigInteger::negative)
47 os << '-';
48 os << x.getMagnitude();
49 return os;
50 }
OLDNEW
« no previous file with comments | « third_party/bigint/BigIntegerLibrary.hh ('k') | third_party/bigint/BigIntegerUtils.hh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698