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

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

Start of work to make the pablo compiler reenterant. Fixed bug that prevented it from using Less optimization level.

File size: 4.5 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 "PropertyObjectTable.h"
10#include <sstream>
11#include <algorithm>
12
13namespace UCD {
14
15std::string canonicalize_value_name(const std::string & prop_or_val) {
16    std::locale loc;
17    std::stringstream s;
18    for (char c : prop_or_val) {
19        if ((c != '_') && (c != ' ') && (c != '-')) {
20            s << std::tolower(c, loc);
21        }
22    }
23    return s.str();
24}
25
26int PropertyObject::GetPropertyValueEnumCode(const std::string & value_spec) {
27    throw std::runtime_error("Property " + value_spec + " unsupported.");
28}
29
30UnicodeSet UnsupportedPropertyObject::GetCodepointSet(const std::string &) {
31    throw std::runtime_error("Property " + UCD::property_full_name[the_property] + " unsupported.");
32}
33
34UnicodeSet UnsupportedPropertyObject::GetCodepointSet(const int) {
35    throw std::runtime_error("Property " + UCD::property_full_name[the_property] + " unsupported.");
36}
37
38const UnicodeSet & EnumeratedPropertyObject::GetCodepointSet(const std::string & value_spec) {
39    int property_enum_val = GetPropertyValueEnumCode(value_spec);
40    if (property_enum_val == -1) {
41        throw std::runtime_error("Enumerated Property " + UCD::property_full_name[the_property] +  ": unknown value: " + value_spec);
42    }
43    return GetCodepointSet(property_enum_val);
44}
45
46const UnicodeSet & EnumeratedPropertyObject::GetCodepointSet(const int property_enum_val) const {
47    assert (property_enum_val >= 0);
48    return *(property_value_sets[property_enum_val]);
49}
50
51int EnumeratedPropertyObject::GetPropertyValueEnumCode(const std::string & value_spec) {
52    // The canonical full names are not stored in the precomputed alias map,
53    // to save space in the executable.   Add them if the property is used.
54    if (uninitialized) {
55        for (unsigned i = 0; i != property_value_full_names.size(); i++) {
56            property_value_aliases.insert({canonicalize_value_name(property_value_full_names[i]), i});
57        }
58        for (unsigned i = 0; i != property_value_enum_names.size(); i++) {
59            property_value_aliases.insert({canonicalize_value_name(property_value_enum_names[i]), i});
60        }
61        uninitialized = false;
62    }
63    const auto valit = property_value_aliases.find(value_spec);
64    if (valit == property_value_aliases.end())
65        return -1;
66    return valit->second;
67}
68
69PropertyObject::iterator ExtensionPropertyObject::begin() const {
70    if (const auto * obj = dyn_cast<EnumeratedPropertyObject>(property_object_table[base_property])) {
71        return obj->begin();
72    }
73    throw std::runtime_error("Iterators unsupported for this type of PropertyObject.");
74}
75
76PropertyObject::iterator ExtensionPropertyObject::end() const {
77    if (const auto * obj = dyn_cast<EnumeratedPropertyObject>(property_object_table[base_property])) {
78        return obj->end();
79    }
80    throw std::runtime_error("Iterators unsupported for this type of PropertyObject.");
81}
82
83const UnicodeSet & ExtensionPropertyObject::GetCodepointSet(const std::string & value_spec) {
84    int property_enum_val = GetPropertyValueEnumCode(value_spec);
85    if (property_enum_val == -1) {
86        throw std::runtime_error("Extension Property " + UCD::property_full_name[the_property] +  ": unknown value: " + value_spec);
87    }
88    return GetCodepointSet(property_enum_val);
89}
90
91const UnicodeSet & ExtensionPropertyObject::GetCodepointSet(const int property_enum_val) const {
92    assert (property_enum_val >= 0);
93    return *(property_value_sets[property_enum_val]);
94}
95
96int ExtensionPropertyObject::GetPropertyValueEnumCode(const std::string & value_spec) {
97    return property_object_table[base_property]->GetPropertyValueEnumCode(value_spec);
98}
99
100UnicodeSet BinaryPropertyObject::GetCodepointSet(const std::string & value_spec) const {
101    if (value_spec.length() != 0) {
102        auto valit = Binary_ns::aliases_only_map.find(value_spec);
103        if (valit == Binary_ns::aliases_only_map.end()) {
104            throw std::runtime_error("Binary Property " + UCD::property_full_name[the_property] +  ": bad value: " + value_spec);
105        }
106        if (valit->second == Binary_ns::Y)
107            return the_codepoint_set;
108        return ~the_codepoint_set;
109    }
110    return the_codepoint_set;
111}
112
113UnicodeSet BinaryPropertyObject::GetCodepointSet(const int property_enum_val) const {
114    if (property_enum_val == Binary_ns::Y)
115        return the_codepoint_set;
116    return ~the_codepoint_set;
117}
118
119}
Note: See TracBrowser for help on using the repository browser.