]> git.tdb.fi Git - libs/core.git/blob - source/strings/format.cpp
1b8b4650a9efc852e2ab42268c775d7f5cb858f1
[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 const string &Formatter::str() const
15 {
16         if(pos!=fmt.end())
17                 throw format_error("Too few arguments for format");
18
19         return result;
20 }
21
22 void Formatter::advance()
23 {
24         for(; pos!=fmt.end(); ++pos)
25         {
26                 if(*pos=='%')
27                 {
28                         ++pos;
29                         if(pos==fmt.end())
30                                 throw format_error("Malformed format string");
31                         if(*pos!='%')
32                                 break;
33                 }
34
35                 result += *pos;
36         }
37 }
38
39 Fmt Formatter::get_conversion()
40 {
41         if(pos==fmt.end())
42                 throw format_error("Too many arguments for format");
43
44         string::iterator i = pos;
45         for(; i!=fmt.end(); ++i)
46                 if(isalpha(*i))
47                         break;
48
49         if(i==fmt.end())
50                 throw format_error("Malformed format string");
51
52         ++i;
53         string c(pos, i);
54         pos = i;
55
56         return Fmt(c);
57 }
58
59 } // namespace Msp