Parser

class Parser(action: Pair<Iterable<Char>, DecoderAction>)(source)

Holder for ranges of characters and their associated DecoderAction with support for constant-time operations (in terms of work performed for provided input).

e.g. (NOT constant-time operation)

fun String.containsChar(char: Char): Boolean {
    for (c in this) {
        if (c == char) return true
    }
    return false
}

e.g. (YES constant-time operation)

fun String.containsChar(char: Char): Boolean {
    var result = false
    for (c in this) {
        result = if (c == char) true else result
    }
    return result
}

e.g. (Base16)

val parser = DecoderAction.Parser(
    '0'..'9' to DecoderAction { char ->
        char.code - 48
    },
    'A'..'Z' to DecoderAction { char ->
        char.code - 55
    }
)

val bits = "48656C6C6F20576F726C6421".map { char ->
    parser.parse(char, isConstantTime = true)
        ?: error("Invalid Char[$char]")
}

Parameters

action

Pairs of character ranges and their associated DecoderAction

Constructors

Link copied to clipboard
constructor(vararg action: Pair<Iterable<Char>, DecoderAction>)

Properties

Link copied to clipboard
@JvmField
val actions: List<Pair<Set<Char>, DecoderAction>>

Functions

Link copied to clipboard
fun parse(input: Char, isConstantTime: Boolean): Int?

Parses the actions for containing input, invoking the DecoderAction.convert for that associated range of characters.