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

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

Temporary check-in.

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