scide: implement selectionLength for openDocument
[supercollider.git] / external_libraries / yaml-cpp-0.3.0 / src / conversion.cpp
blobf81e1a0b0cbab9e09f55170bed5ada3076854720
1 #include "yaml-cpp/conversion.h"
2 #include <algorithm>
4 ////////////////////////////////////////////////////////////////
5 // Specializations for converting a string to specific types
7 namespace
9 // we're not gonna mess with the mess that is all the isupper/etc. functions
10 bool IsLower(char ch) { return 'a' <= ch && ch <= 'z'; }
11 bool IsUpper(char ch) { return 'A' <= ch && ch <= 'Z'; }
12 char ToLower(char ch) { return IsUpper(ch) ? ch + 'a' - 'A' : ch; }
14 std::string tolower(const std::string& str)
16 std::string s(str);
17 std::transform(s.begin(), s.end(), s.begin(), ToLower);
18 return s;
21 template <typename T>
22 bool IsEntirely(const std::string& str, T func)
24 for(std::size_t i=0;i<str.size();i++)
25 if(!func(str[i]))
26 return false;
28 return true;
31 // IsFlexibleCase
32 // . Returns true if 'str' is:
33 // . UPPERCASE
34 // . lowercase
35 // . Capitalized
36 bool IsFlexibleCase(const std::string& str)
38 if(str.empty())
39 return true;
41 if(IsEntirely(str, IsLower))
42 return true;
44 bool firstcaps = IsUpper(str[0]);
45 std::string rest = str.substr(1);
46 return firstcaps && (IsEntirely(rest, IsLower) || IsEntirely(rest, IsUpper));
50 namespace YAML
52 bool Convert(const std::string& input, bool& b)
54 // we can't use iostream bool extraction operators as they don't
55 // recognize all possible values in the table below (taken from
56 // http://yaml.org/type/bool.html)
57 static const struct {
58 std::string truename, falsename;
59 } names[] = {
60 { "y", "n" },
61 { "yes", "no" },
62 { "true", "false" },
63 { "on", "off" },
66 if(!IsFlexibleCase(input))
67 return false;
69 for(unsigned i=0;i<sizeof(names)/sizeof(names[0]);i++) {
70 if(names[i].truename == tolower(input)) {
71 b = true;
72 return true;
75 if(names[i].falsename == tolower(input)) {
76 b = false;
77 return true;
81 return false;
84 bool Convert(const std::string& input, _Null& /*output*/)
86 return input.empty() || input == "~" || input == "null" || input == "Null" || input == "NULL";