Category talk:Wren-ioutil: Difference between revisions

→‎Source code: Added Input.readHexBytes method.
m (→‎Source code: Now uses Wren S/H lexer.)
(→‎Source code: Added Input.readHexBytes method.)
 
Line 545:
// Convenience version of the above method which always allows symbols.
static password(prompt, minLen, maxLen) { password(prompt, minLen, maxLen, true) }
 
// Reads 1, 2, 3 or 4 hex bytes (2 digits per byte, upper/lower case a-f, big endian)
// entered at the terminal and returns the decimal value of the byte group entered.
// Returns -1 if the return key is pressed after a byte group to signify end of input.
// Returns -2 if the space or tab key is pressed afer a byte group which enables
// calling code to skip intervening whitespace.
// Pressing these keys in the middle of byte groups or entering other non-hex
// characters is an error.
static readHexBytes(nBytes) {
if (!(1..4).contains(nBytes)) {
Fiber.abort("Invalid value for nBytes, must be 1, 2, 3 or 4.")
}
var nDigits = nBytes * 2
var hl = List.filled(nDigits, 0)
for (i in 0...nDigits) {
var h = Stdin.readByte()
if (h == 10) {
if (i == 0) return -1
Fiber.abort("Invalid hex digit entered.")
}
if (h == 32 || h == 9) {
if (i == 0) return -2
Fiber.abort("Invalid hex digit entered.")
}
if (h >= 48 && h <= 57) {
h = h - 48
} else if (h >= 97 && h <= 102) {
h = h - 87
} else if (h >= 65 && h <= 70) {
h = h - 55
} else {
Fiber.abort("Invalid hex digit entered.")
}
hl[i] = h
}
var sum = hl[0]
for (i in 1...hl.count) sum = sum * 16 + hl[i]
return sum
}
 
// Convenience version of readHexBytes method which reads a single hex byte.
static readHexByte() { readHexBytes(1) }
}
 
9,476

edits