]> git.tdb.fi Git - libs/core.git/blob - source/strings/format.cpp
Add move semantics to Variant
[libs/core.git] / source / strings / format.cpp
1 #include "format.h"
2
3 using namespace std;
4
5 namespace Msp {
6
7 Formatter::Formatter(const string &f):
8         fmt(f),
9         pos(fmt.begin())
10 {
11         advance();
12 }
13
14 Formatter::Formatter(const Formatter &other):
15         fmt(other.fmt),
16         pos(fmt.begin()+(other.pos-other.fmt.begin())),
17         result(other.result)
18 { }
19
20 Formatter &Formatter::operator=(const Formatter &other)
21 {
22         if(&other==this)
23                 return *this;
24
25         fmt = other.fmt;
26         pos = fmt.begin()+(other.pos-other.fmt.begin());
27         result = other.result;
28         return *this;
29 }
30
31 const string &Formatter::str() const
32 {
33         if(pos!=fmt.end())
34                 throw format_error("Too few arguments for format");
35
36         return result;
37 }
38
39 void Formatter::advance()
40 {
41         for(; pos!=fmt.end(); ++pos)
42         {
43                 if(*pos=='%')
44                 {
45                         ++pos;
46                         if(pos==fmt.end())
47                                 throw format_error("Malformed format string");
48                         if(*pos!='%')
49                                 break;
50                 }
51
52                 result += *pos;
53         }
54 }
55
56 Fmt Formatter::get_conversion()
57 {
58         if(pos==fmt.end())
59                 throw format_error("Too many arguments for format");
60
61         auto i = pos;
62         for(; i!=fmt.end(); ++i)
63                 if(isalpha(*i))
64                         break;
65
66         if(i==fmt.end())
67                 throw format_error("Malformed format string");
68
69         ++i;
70         string c(pos, i);
71         pos = i;
72
73         return Fmt(c);
74 }
75
76 } // namespace Msp