summaryrefslogtreecommitdiff
path: root/libformat/libformat/format.cxx
blob: 6450093247ef38bebd835f48fe56380860b9c3eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <libformat/format.hxx>

#include <cctype>     // to{upper,lower}()
#include <algorithm>  // transform()
#include <stdexcept>

using namespace std;

namespace format
{
  string
  format_hello (const string& g, const string& n, volume v)
  {
    if (const char* w = (g.empty () ? "empty greeting" :
                         n.empty () ? "empty name"     : nullptr))
      throw invalid_argument (w);

    string h (g);

    transform (h.begin (), h.end (), h.begin (),
               [v] (char c) -> char
               {
                 switch (v)
                 {
                 case volume::quiet:  return tolower (c);
                 case volume::normal: break;
                 case volume::loud:   return toupper (c);
                 }
                 return c;
               });

    return h += ", " + n + '!';
  }
}