module Novika::Quote

Overview

Includers are Novika quotes, which are known as strings in most other programming languages.

Quotes are optimized for the ASCII-only case. Also, they treat single String::Graphemes separately from strings of those, and cache grapheme counts here and there.

The slowest operations on quotes are #slice_at and #at over non- ASCII quotes. Both are currently O(N) in terms of iterations only; they do a lot of other work besides iteration as well.

When you are slow, being even slower doesn't matter that much anymore. This is the case with Novika.

And yes, quotes do rely on the experimental grapheme API.

Included Modules

Direct including types

Defined in:

novika/forms/quote.cr

Constant Summary

EFFECT_BOUND_TAKE = 12

Specifies how many characters to take from the left and right boundaries of the quote for a shorter representation in #effect.

EFFECT_MAX_CHARS = 32

Specifies the maximum amount of characters to display before the quote gets cut off in #effect (see Form#effect).

EMPTY = StringQuote.new("", count: 0, ascii_only: true)

The empty quote.

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Novika::Form

a(type : T.class) : T forall T a, desc(io : IO)
desc : String
desc
, die(details : String) die, effect(io)
effect
effect
, on_open(engine : Engine) : self on_open, on_parent_open(engine : Engine) : self on_parent_open, onto(block : Block) : self onto, sel(a, b) sel, to_quote : Quote to_quote

Instance methods inherited from module Novika::Schedulable

schedule(engine : Engine, stack : Block) schedule, schedule!(engine : Engine, stack : Block) schedule!

Constructor Detail

def self.new(string : String, count = string.grapheme_size, ascii = string.ascii_only?) #

Creates a quote from string.

count can be provided if the amount of graphemes in string is known.


def self.new(grapheme : String::Grapheme) #

Creates a quote from grapheme.


def self.new(char : Char) #

Creates a quote from char.


Class Method Detail

def self.typedesc #

Instance Method Detail

abstract def ==(other : Quote) : Bool #

Returns whether this quote variant consists of the same graphemes as other.


abstract def at(b : Int32, e : Int32) : Quote #

Returns a subquote from b to e. Clamps b and e to bounds of this quote. Returns an empty quote if this quote is empty without regarding b and e.

Both ends are inclusive.


def at(index : Int32) : GraphemeQuote #

Returns the grapheme at index as Quote, or dies.


abstract def at?(index : Int32) : GraphemeQuote | Nil #

Returns the grapheme at index as Quote, or nil.


abstract def cached_count? : Int32 | Nil #

Returns the cached count of graphemes in this quote variant. Returns nil if there is no cached count.


abstract def count : Int32 #

Returns the amount of graphemes in this quote variant.


def desc(io : IO) #
Description copied from module Novika::Form

Appends a string description of this form to io.


def each_occurrence_of(pattern : Form, &) #

Yields occurrences of the given pattern in this quote.


def effect(io) #
Description copied from module Novika::Form

Generates and returns a description for the stack effect of this form.

For blocks and builtins, tries to extract a ( ... -- ... ) (but see EFFECT_PATTERN) from their corresponding comment. If could not extract or no comment, returns 'a block' for blocks and 'native code' for builtins.


abstract def empty? : Bool #

Returns whether this quote is empty.


def first_byte? : UInt8 | Nil #

Returns the first byte (or nil) in this quote.


abstract def fit(total : Int, ellipsis : _) : Quote #

Ensures this quote is of total characters or less. In case of overflow, truncates with ellipsis. If even ellipsis cannot fit, truncates ellipsis so that it is of total characters. Returns the resulting quote.


def ord? : Int32 | Nil #

Returns the Unicode codepoint for the first character in this quote, or nil if this quote is empty.


abstract def pad(total : Int, padder : _, side : PadSide) : Quote #

Pads this quote with padder until it becomes total perceived characters long. The side where the padding should apply is specified by side. Returns the resulting quote.


abstract def replace_all(pattern : Quote, repl : Quote) : Quote #

Replaces instances of pattern with repl. Returns the resulting quote.


def slice_at(slicept : Int32) : Tuple(Quote, Quote) #

Slices this quote into two quotes at slicept. Returns the two resulting quotes. Dies if slicept is out of bounds.


def slice_at?(slicept : Int32) : Tuple(Quote, Quote) | Nil #

Slices this quote into two quotes at slicept. Returns the two resulting quotes. Returns nil if slicept is out of bounds.


def stitch(other : Quote) : Quote #

Stitches (concatenates) this and other quote variants, and returns the resulting quote.


abstract def string : String #

Converts this quote variant to String.


def to_byteslice #

Returns an immutable Byteslice representation of this quote.


def to_quote : Quote #
Description copied from module Novika::Form

Returns this form's quote representation.