1 // Copyright 2014 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code by Matt McCutchen, see the LICENSE file. 6 7 #ifndef BIGUNSIGNEDINABASE_H 8 #define BIGUNSIGNEDINABASE_H 9 10 #include "NumberlikeArray.hh" 11 #include "BigUnsigned.hh" 12 #include <string> 13 14 /* 15 * A BigUnsignedInABase object represents a nonnegative integer of size limited 16 * only by available memory, represented in a user-specified base that can fit 17 * in an `unsigned short' (most can, and this saves memory). 18 * 19 * BigUnsignedInABase is intended as an intermediary class with little 20 * functionality of its own. BigUnsignedInABase objects can be constructed 21 * from, and converted to, BigUnsigneds (requiring multiplication, mods, etc.) 22 * and `std::string's (by switching digit values for appropriate characters). 23 * 24 * BigUnsignedInABase is similar to BigUnsigned. Note the following: 25 * 26 * (1) They represent the number in exactly the same way, except that 27 * BigUnsignedInABase uses ``digits'' (or Digit) where BigUnsigned uses 28 * ``blocks'' (or Blk). 29 * 30 * (2) Both use the management features of NumberlikeArray. (In fact, my desire 31 * to add a BigUnsignedInABase class without duplicating a lot of code led me to 32 * introduce NumberlikeArray.) 33 * 34 * (3) The only arithmetic operation supported by BigUnsignedInABase is an 35 * equality test. Use BigUnsigned for arithmetic. 36 */ 37 38 class BigUnsignedInABase : protected NumberlikeArray<unsigned short> { 39 40 public: 41 // The digits of a BigUnsignedInABase are unsigned shorts. 42 typedef unsigned short Digit; 43 // That's also the type of a base. 44 typedef Digit Base; 45 46 protected: 47 // The base in which this BigUnsignedInABase is expressed 48 Base base; 49 50 // Creates a BigUnsignedInABase with a capacity; for internal use. BigUnsignedInABase(int,Index c)51 BigUnsignedInABase(int, Index c) : NumberlikeArray<Digit>(0, c) {} 52 53 // Decreases len to eliminate any leading zero digits. zapLeadingZeros()54 void zapLeadingZeros() { 55 while (len > 0 && blk[len - 1] == 0) 56 len--; 57 } 58 59 public: 60 // Constructs zero in base 2. BigUnsignedInABase()61 BigUnsignedInABase() : NumberlikeArray<Digit>(), base(2) {} 62 63 // Copy constructor BigUnsignedInABase(const BigUnsignedInABase & x)64 BigUnsignedInABase(const BigUnsignedInABase &x) : NumberlikeArray<Digit>(x), base(x.base) {} 65 66 // Assignment operator operator =(const BigUnsignedInABase & x)67 BigUnsignedInABase& operator =(const BigUnsignedInABase &x) { 68 NumberlikeArray<Digit>::operator =(x); 69 base = x.base; 70 return *this; 71 } 72 73 // Constructor that copies from a given array of digits. 74 BigUnsignedInABase(const Digit *d, Index l, Base base); 75 76 // Destructor. NumberlikeArray does the delete for us. ~BigUnsignedInABase()77 ~BigUnsignedInABase() {} 78 79 // LINKS TO BIGUNSIGNED 80 BigUnsignedInABase(const BigUnsigned &x, Base base); 81 operator BigUnsigned() const; 82 83 /* LINKS TO STRINGS 84 * 85 * These use the symbols ``0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'' to 86 * represent digits of 0 through 35. When parsing strings, lowercase is 87 * also accepted. 88 * 89 * All string representations are big-endian (big-place-value digits 90 * first). (Computer scientists have adopted zero-based counting; why 91 * can't they tolerate little-endian numbers?) 92 * 93 * No string representation has a ``base indicator'' like ``0x''. 94 * 95 * An exception is made for zero: it is converted to ``0'' and not the 96 * empty string. 97 * 98 * If you want different conventions, write your own routines to go 99 * between BigUnsignedInABase and strings. It's not hard. 100 */ 101 operator std::string() const; 102 BigUnsignedInABase(const std::string &s, Base base); 103 104 public: 105 106 // ACCESSORS getBase() const107 Base getBase() const { return base; } 108 109 // Expose these from NumberlikeArray directly. 110 using NumberlikeArray<Digit>::getCapacity; 111 using NumberlikeArray<Digit>::getLength; 112 113 /* Returns the requested digit, or 0 if it is beyond the length (as if 114 * the number had 0s infinitely to the left). */ getDigit(Index i) const115 Digit getDigit(Index i) const { return i >= len ? 0 : blk[i]; } 116 117 // The number is zero if and only if the canonical length is zero. isZero() const118 bool isZero() const { return NumberlikeArray<Digit>::isEmpty(); } 119 120 /* Equality test. For the purposes of this test, two BigUnsignedInABase 121 * values must have the same base to be equal. */ operator ==(const BigUnsignedInABase & x) const122 bool operator ==(const BigUnsignedInABase &x) const { 123 return base == x.base && NumberlikeArray<Digit>::operator ==(x); 124 } operator !=(const BigUnsignedInABase & x) const125 bool operator !=(const BigUnsignedInABase &x) const { return !operator ==(x); } 126 127 }; 128 129 #endif 130