/* */ #ifndef _D_ALPHA_NUMBER_DECORATOR_H_ #define _D_ALPHA_NUMBER_DECORATOR_H_ #include "NumberDecorator.h" #include "DlAbortEx.h" namespace aria2 { class AlphaNumberDecorator : public NumberDecorator { private: int32_t _width; std::string _zero; std::string widen(const std::string& s, int32_t width) { std::string t = s; while(t.size() < (size_t)width) { t.insert(0, _zero); } return t; } public: AlphaNumberDecorator(int32_t width, bool uppercase = false): _width(width), _zero(uppercase?"A":"a") {} virtual ~AlphaNumberDecorator() {} virtual std::string decorate(int32_t number) { if(number < 0) { throw new DlAbortEx("The number must be greater than 0."); } if(number == 0) { return widen(_zero, _width); } int32_t base = 26; std::string x; while(number > 0) { int32_t r = number%base; char alpha = _zero[0]+r; x.insert(0, std::string(1, alpha)); number /= base; } return widen(x, _width); } }; } // namespace aria2 #endif // _D_ALPHA_NUMBER_DECORATOR_H_