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

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

Temporary check-in for dynamic unicode class compilation.

File size: 3.0 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
24UnicodeSet UnsupportedPropertyObject::GetCodepointSet(const std::string &) {
25    throw std::runtime_error("Property " + UCD::property_full_name[the_property] + " unsupported.");
26}
27
28UnicodeSet UnsupportedPropertyObject::GetCodepointSet(const int) {
29    throw std::runtime_error("Property " + UCD::property_full_name[the_property] + " unsupported.");
30}
31
32UnicodeSet EnumeratedPropertyObject::GetCodepointSet(const std::string & value_spec) {
33    int property_enum_val = GetPropertyValueEnumCode(value_spec);
34    if (property_enum_val == -1) {
35        throw std::runtime_error("Enumerated Property " + UCD::property_full_name[the_property] +  ": unknown value: " + value_spec);
36    }
37    return GetCodepointSet(property_enum_val);
38}
39
40UnicodeSet EnumeratedPropertyObject::GetCodepointSet(const int property_enum_val) const {
41    assert (property_enum_val >= 0);
42    return property_value_sets[property_enum_val];
43}
44
45int EnumeratedPropertyObject::GetPropertyValueEnumCode(const std::string & value_spec) {
46    // The canonical full names are not stored in the precomputed alias map,
47    // to save space in the executable.   Add them if the property is used.
48    if (!aliases_initialized) {
49        for (unsigned i = 0; i != property_value_full_names.size(); i++) {
50            property_value_aliases.insert({canonicalize_value_name(property_value_full_names[i]), i});
51        }
52        for (unsigned i = 0; i != property_value_enum_names.size(); i++) {
53            property_value_aliases.insert({canonicalize_value_name(property_value_enum_names[i]), i});
54        }
55        aliases_initialized = true;
56    }
57    const auto valit = property_value_aliases.find(value_spec);
58    if (valit == property_value_aliases.end())
59        return -1;
60    return valit->second;
61}
62
63UnicodeSet BinaryPropertyObject::GetCodepointSet(const std::string & value_spec) const {
64    if (value_spec.length() != 0) {
65        auto valit = Binary_ns::aliases_only_map.find(value_spec);
66        if (valit == Binary_ns::aliases_only_map.end()) {
67            throw std::runtime_error("Binary Property " + UCD::property_full_name[the_property] +  ": bad value: " + value_spec);
68        }
69        if (valit->second == Binary_ns::Y)
70            return the_codepoint_set;
71        return ~the_codepoint_set;
72    }
73    return the_codepoint_set;
74}
75
76UnicodeSet BinaryPropertyObject::GetCodepointSet(const int property_enum_val) const {
77    if (property_enum_val == Binary_ns::Y)
78        return the_codepoint_set;
79    return ~the_codepoint_set;
80}
81
82}
Note: See TracBrowser for help on using the repository browser.