source: icGREP/icgrep-devel/icgrep/UCD/PropertyObjects.cpp @ 4618

Last change on this file since 4618 was 4618, checked in by nmedfort, 4 years ago

More modifications to UnicodeSet? class.

File size: 2.6 KB
Line 
1/*
2 *  Copyright (c) 2014 International Characters, Inc.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters, Inc.
5 *
6 */
7
8#include "PropertyObjects.h"
9#include <sstream>
10
11namespace UCD {
12
13std::string canonicalize_value_name(const std::string prop_or_val) {
14    std::locale loc;
15    std::stringstream s;
16    for (char c : prop_or_val) {
17        if ((c != '_') && (c != ' ') && (c != '-')) {
18            s << std::tolower(c, loc);
19        }
20    }
21    return s.str();
22}
23
24
25UnicodeSet UnsupportedPropertyObject::GetCodepointSet(const std::string value_spec) {
26    throw std::runtime_error("Property " + UCD::property_full_name[the_property] + " unsupported.");
27}
28
29UnicodeSet EnumeratedPropertyObject::GetCodepointSet(const std::string value_spec) {
30    int property_enum_val = GetPropertyValueEnumCode(value_spec);
31    if (property_enum_val == -1) {
32            throw std::runtime_error("Enumerated Property " + UCD::property_full_name[the_property] +  ": unknown value: " + value_spec);
33    }
34    else {
35        return property_value_sets[property_enum_val];
36    }
37}
38
39int EnumeratedPropertyObject::GetPropertyValueEnumCode(const std::string s) {
40    // The canonical full names are not stored in the precomputed alias map,
41    // to save space in the executable.   Add them if the property is used.
42    if (!aliases_initialized) {
43        for (auto v = 0; v < property_value_full_names.size(); v++) {
44            property_value_aliases.insert({canonicalize_value_name(property_value_full_names[v]), v});
45        }
46        for (auto v = 0; v < property_value_enum_names.size(); v++) {
47            property_value_aliases.insert({canonicalize_value_name(property_value_enum_names[v]), v});
48        }
49        aliases_initialized = true;
50    }
51    auto valit = property_value_aliases.find(s);
52    if (valit == property_value_aliases.end()) return -1;
53    else return valit->second;
54}
55
56UnicodeSet BinaryPropertyObject::GetCodepointSet(const std::string value_spec) {
57    int property_enum_val = Binary_ns::Y; // default value
58    if (value_spec != "") {
59        auto valit = Binary_ns::aliases_only_map.find(value_spec);
60        if (valit == Binary_ns::aliases_only_map.end()) {
61                    throw std::runtime_error("Binary Property " + UCD::property_full_name[the_property] +  ": bad value: " + value_spec);
62        }
63        else property_enum_val = valit->second;
64        if (property_enum_val == Binary_ns::Y) return the_codepoint_set;
65        else return uset_complement(the_codepoint_set);
66    }
67    else return the_codepoint_set;
68}
69
70
71}
Note: See TracBrowser for help on using the repository browser.