monotone

monotone Mtn Source Tree

Root/specialized_lexical_cast.cc

1// Copyright (C) 2007 Timothy Brownawell <tbrownaw@gmail.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10
11#include "base.hh"
12#include "lexical_cast.hh"
13
14template<>
15std::string boost::lexical_cast<std::string, unsigned int>(unsigned int const & _i)
16{
17 unsigned int i = _i;
18 if (!i)
19 return std::string("0");
20
21 unsigned int const maxlen = sizeof(unsigned int) * 3;
22 char buf[1+maxlen];
23 buf[maxlen] = '\0';
24
25 unsigned int pos = maxlen;
26
27 while (i && pos <= maxlen)
28 {
29 --pos;
30 buf[pos] = ('0' + (i%10));
31 i /= 10;
32 }
33 return std::string(buf+pos);
34}
35
36template<>
37unsigned int boost::lexical_cast<unsigned int, std::string>(std::string const & s)
38{
39 unsigned int out = 0;
40 std::string::const_iterator i;
41 for (i = s.begin(); i != s.end() && (unsigned int)(*i - '0') < 10; ++i)
42 {
43 out = out*10 + (*i - '0');
44 }
45 if (i != s.end())
46 throw boost::bad_lexical_cast();
47 return out;
48}
49
50// Local Variables:
51// mode: C++
52// fill-column: 76
53// c-file-style: "gnu"
54// indent-tabs-mode: nil
55// End:
56// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status