10#ifndef __UTILS_UNITS_SUBSTITUTE_H
11#define __UTILS_UNITS_SUBSTITUTE_H
13#include "TextInterfaceName.h"
22template<
class Pred> std::string::size_type
23find_first_if (
const std::string& text, Pred pred, std::string::size_type pos)
25 std::string::const_iterator iter;
26 iter = std::find_if (text.begin()+pos, text.end(), pred);
27 if (iter == text.end())
28 return std::string::npos;
30 return iter - text.begin();
34template<
class Pred> std::string::size_type
35find_last_if (
const std::string& text, Pred pred)
37 auto iter = std::find_if (text.rbegin(), text.rend(), pred);
38 if (iter == text.rend())
39 return std::string::npos;
41 return text.length() - (iter - text.rbegin());
45std::string substitute (
const std::string& text, T* resolver,
46 char substitution =
'$',
53 throw Error (InvalidState,
"substitute",
"resolver = NULL");
55 std::string remain = text;
58 std::string::size_type start;
60 while ( (start = remain.find(substitution)) != std::string::npos ) {
63 std::string before = remain.substr (0, start);
69 std::string::size_type name_start = start;
70 while (name_start < remain.length() && remain[name_start] == substitution)
74 while (remain[name_start] ==
'<')
76 std::string::size_type command_start = name_start + 1;
77 std::string::size_type command_end = remain.find(
'>',name_start);
78 if (command_end == std::string::npos)
79 throw Error (InvalidState,
"substitute",
"command opening '<' without closing '>'");
81 std::string command = remain.substr (command_start, command_end - command_start);
83 resolver->process (command);
84 start = name_start = command_end + 1;
88 std::string::size_type end;
89 end = find_first_if (remain, std::not1(in_name), name_start);
92 std::string::size_type length = std::string::npos;
94 if (end != std::string::npos)
98 std::string name = remain.substr (start, length);
100 if (start == name_start)
102 result += before + resolver->get_value(name);
104 result += before + name;
107 if (end != std::string::npos)
108 remain = remain.substr (end);
113 return result + remain;
117 throw error +=
"substitute (text=\"" + text +
"\",parser=" +
118 resolver->get_interface_name() +
")";
A convenient exception handling class.
Definition Error.h:54
Implements an adaptable function object in compliance with the STL.
Definition Functor.h:39
Defines the sequence of characters that constitute a valid name.
Definition TextInterfaceName.h:20
bool valid(char c) const
Return true if c is the next character in a valid name.
Definition TextInterfaceName.C:22