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

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

GCC fix + misc. changes

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