Something went wrong. Try again.
🪄 dd's hex editor
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118/// Implements range expressions.////// Copyright: dd86k <dd@dax.moe>/// License: MIT/// Authors: $(LINK2 https://github.com/dd86k, dd86k)module ranges;
import messages;import utils : scan;
/// Special valuesenum RangeSentinel { eof = -1, /// End of file (document): '$' cursor = -2, /// Cursor position: '.'}enum { RANGE_RELATIVE = 1, /// If set, end is a length value}
/// Represents an inclusive range.struct Range{ long start; /// Start. long end; /// End or length if RANGE_RELATIVE is set. int flags; /// Flags. long length() { return end - start + 1; }}/// Parse a range expression.////// Only '$' is accepted as an end portion that turns into -1,/// for special designation to the caller./// Params: expr = Expression string./// Returns: Range structure./// Throws: Exception.Range range(string expr){ if (expr is null || expr.length == 0) throw new Exception(MSG_EMPTY_RANGE); import std.string : indexOf; Range r; ptrdiff_t i = indexOf(expr, ':'); if (i < 0) { // Assume length-only range expression if separator missing r.start = RangeSentinel.cursor; r.end = scan(expr); if (r.end == 0) throw new Exception(MSG_RANGE_LENGTH_ZERO); r.end--; r.flags = RANGE_RELATIVE; return r; } if (i == 0) throw new Exception(MSG_RANGE_MISSING_START); if (i+1 == expr.length) throw new Exception(MSG_RANGE_MISSING_END); // Process start string a = expr[0..i]; switch (a) { case "$": r.start = RangeSentinel.eof; break; case ".": r.start = RangeSentinel.cursor; break; default: r.start = scan(a); } // Process end string b = expr[i+1..$]; switch (b) { case "$": r.end = RangeSentinel.eof; break; case ".": r.end = RangeSentinel.cursor; break; default: if (b[0] == '+') { if (b.length < 2) throw new Exception(MSG_RANGE_MISSING_LENGTH); r.end = scan(b[1..$]); r.flags = RANGE_RELATIVE; } else r.end = scan(b); } return r;}unittest{ // Empty try { cast(void)range(""); assert(false); } catch (Exception) {} // Separator only try { cast(void)range(":"); assert(false); } catch (Exception) {} // Missing end try { cast(void)range("0:"); assert(false); } catch (Exception) {} try { cast(void)range("0:+"); assert(false); } catch (Exception) {} // Missing start try { cast(void)range(":$"); assert(false); } catch (Exception) {} assert(range("0:0") == Range( 0, 0 )); assert(range("0:1") == Range( 0, 1 )); assert(range("1:1") == Range( 1, 1 )); assert(range("5:25") == Range( 5, 25 )); assert(range("010:0x30") == Range( 8, 0x30 )); assert(range("5:$") == Range( 5, RangeSentinel.eof )); assert(range("$:0x20") == Range( RangeSentinel.eof, 0x20 )); assert(range(".:0x20") == Range( RangeSentinel.cursor, 0x20 )); assert(range(".:$") == Range( RangeSentinel.cursor, RangeSentinel.eof )); // Relative assert(range(".:+0x100") == Range( RangeSentinel.cursor, 0x100, RANGE_RELATIVE )); assert(range("0x100") == Range( RangeSentinel.cursor, 0x100-1, RANGE_RELATIVE ));}