source: icGREP/icgrep-devel/icgrep/cc/alphabet.cpp @ 5800

Last change on this file since 5800 was 5800, checked in by cameron, 13 months ago

isa and dyn_cast for Alphabets

File size: 1.7 KB
Line 
1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7
8#include "alphabet.h"
9#include <llvm/Support/ErrorHandling.h>
10
11namespace cc {
12   
13UnicodeMappableAlphabet::UnicodeMappableAlphabet(std::string alphabetName,
14                                                 unsigned unicodeCommon,
15                                                 std::vector <UCD::codepoint_t> aboveCommon) :
16    Alphabet(alphabetName, ClassTypeId::UnicodeMappableAlphabet),
17    mUnicodeCommon(unicodeCommon),
18    mAboveCommon(aboveCommon) {}
19
20UCD::codepoint_t UnicodeMappableAlphabet::toUnicode(const unsigned n) const {
21    UCD::codepoint_t cp = n;
22    if (n < mUnicodeCommon) return cp;
23    assert(n < mUnicodeCommon + mAboveCommon.size());
24    return mAboveCommon[n - mUnicodeCommon];
25}
26 
27unsigned UnicodeMappableAlphabet::fromUnicode(const UCD::codepoint_t codepoint) const {
28    unsigned n = codepoint;
29    if (n < mUnicodeCommon) return n;
30    for (unsigned i = 0; i < mAboveCommon.size(); i++) {
31        if (mAboveCommon[i] == codepoint) return mUnicodeCommon + i;
32    }
33    llvm::report_fatal_error("fromUnicode: codepoint not found in alphabet.");
34}
35
36CodeUnitAlphabet::CodeUnitAlphabet(std::string alphabetName, uint8_t bits) :
37    Alphabet(alphabetName, ClassTypeId::CodeUnitAlphabet), mCodeUnitBits(bits) {}
38
39const UnicodeMappableAlphabet Unicode("Unicode", UCD::UNICODE_MAX, {});
40
41const UnicodeMappableAlphabet ASCII("ASCII", 0x7F, {});
42
43const UnicodeMappableAlphabet ISO_Latin1("ISO_Latin1", 0xFF, {});
44
45const CodeUnitAlphabet Byte("Byte", 8);
46   
47const CodeUnitAlphabet UTF16("UTF16", 16);
48   
49}
Note: See TracBrowser for help on using the repository browser.