<commit_before>"""YANG built-in types"""

import base64
from xml.sax.saxutils import quoteattr
from xml.sax.saxutils import escape

try:
    # python 2
    from StringIO import StringIO
except ImportError:
    # python 3
    from io import StringIO

from . import util
from . import syntax
from .error import err_add

class Abort(Exception):
    pass

class TypeSpec(object):
    def __init__(self, name):
        self.definition = ""
        self.name = name
        self.base = None

    def str_to_val(self, errors, pos, string, module):
        return string;

    def validate(self, errors, pos, val, module, errstr=''):
        return True;

    def restrictions(self):
        return []

class IntTypeSpec(TypeSpec):
    def __init__(self, name, minimum, maximum):
        TypeSpec.__init__(self, name)
        self.is_int = True
        self.min = minimum
        self.max = maximum

    def str_to_val(self, errors, pos, string, _module):
        try:
            if len(string) > 1 and string[0] == '0' and string[1] != 'x':
                # positive octal
                string = string[:1] + 'o' + string[1:]
            elif len(string) > 2 and string[0] == '-' and \
                 string[1] == '0' and string[2] != 'x':
                # negative octal
                string = string[:2] + 'o' + string[2:]
            return int(string, 0)
        except ValueError:
            err_add(errors, pos, 'TYPE_VALUE',
                    (string, self.definition, 'not an integer'))
            return None

    def validate(self, errors, pos, val, _module, errstr=''):
        if val < self.min or val > self.max:
            err_add(errors, pos, 'TYPE_VALUE',
                    (str(val), self.definition, 'range error' + errstr))
            return False
        else:
            return True

    def restrictions(self):
        return ['range']

class Decimal64Value(object):
    def __init__(self, value, s=None, fd=None):
        # must set s (string repr) OR fd (fraction-digits)
        self.value = value
        self.s = s
        if s is None:
            if fd is None:
                raise ValueError(
                    'Decimal64 must set s (string) OR fd (fraction-digits)')
            s = str(value)
            fd = int(fd)
            self.s = s[:-fd] + "." + s[-fd:]
<commit_msg>More explicit hex/octal string conversion for int types; report original string in error.<commit_after>"""YANG built-in types"""

import base64
from xml.sax.saxutils import quoteattr
from xml.sax.saxutils import escape

try:
    # python 2
    from StringIO import StringIO
except ImportError:
    # python 3
    from io import StringIO

from . import util
from . import syntax
from .error import err_add

class Abort(Exception):
    pass

class TypeSpec(object):
    def __init__(self, name):
        self.definition = ""
        self.name = name
        self.base = None

    def str_to_val(self, errors, pos, string, module):
        return string;

    def validate(self, errors, pos, val, module, errstr=''):
        return True;

    def restrictions(self):
        return []

class IntTypeSpec(TypeSpec):
    def __init__(self, name, minimum, maximum):
        TypeSpec.__init__(self, name)
        self.is_int = True
        self.min = minimum
        self.max = maximum

    def str_to_val(self, errors, pos, string, _module):
        negative = string.startswith('-')
        base = 10
        start = 0
        if len(string) > negative + 1 and string[negative] == '0':
            second = string[negative + 1]
            if second == 'x':
                base = 16
                start = 2
            else:
                base = 8
                start = 1
        try:
            absolute = int(string[negative + start:], base)
            return -absolute if negative else absolute
        except ValueError:
            err_add(errors, pos, 'TYPE_VALUE',
                    (string, self.definition, 'not an integer'))
            return None

    def validate(self, errors, pos, val, _module, errstr=''):
        if val < self.min or val > self.max:
            err_add(errors, pos, 'TYPE_VALUE',
                    (str(val), self.definition, 'range error' + errstr))
            return False
        else:
            return True

    def restrictions(self):
        return ['range']

class Decimal64Value(object):
    def __init__(self, value, s=None, fd=None):
        # must set s (string repr) OR fd (fraction-digits)
        self.value = value
        self.s = s
        if s is None:
            if fd is None:
                raise ValueError(
                    'Decimal64 must set s (string) OR fd (fraction-digits)')
            s = str(value)
            fd = int(fd)
            self.s = s[:-fd] + "." + s[-fd:]

{'fraction_non_alphanumeric': 0.10747957259585167, 'fraction_numerical': 0.008589985334171381, 'mean_word_length': 1.8621103117505995, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3165452', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1360, 'n_words': 443}