Class: Hegel::Generators::TextGenerator

Inherits:
Hegel::Generator show all
Defined in:
lib/hegel/generators.rb,
sig/hegel.rbs

Overview

Hegel::Syntax::Methods#text. A Unicode string of [min_size, max_size] characters, unbounded above by default.

Instance Method Summary collapse

Methods inherited from Hegel::Generator

#filter, #map

Constructor Details

#initialize(min_size:, max_size:, codec:, min_codepoint:, max_codepoint:) ⇒ TextGenerator

Returns a new instance of TextGenerator.

Parameters:

  • min_size: (Integer)
  • max_size: (Integer, nil)
  • codec: (String, nil)
  • min_codepoint: (Integer, nil)
  • max_codepoint: (Integer, nil)


108
109
110
111
112
113
114
115
# File 'lib/hegel/generators.rb', line 108

def initialize(min_size:, max_size:, codec:, min_codepoint:, max_codepoint:)
  super()
  @min_size = min_size
  @max_size = max_size
  @codec = codec
  @min_codepoint = min_codepoint
  @max_codepoint = max_codepoint
end

Instance Method Details

#do_draw(tc) ⇒ String

Parameters:

  • tc (Object)

Returns:

  • (String)

Raises:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hegel/generators.rb', line 117

def do_draw(tc)
  max_size = @max_size || LibHegel::HEGEL_COLLECTION_MAX_SIZE_UNBOUNDED
  raise Hegel::Error, "text: max_size < min_size" if max_size < @min_size

  # A fresh generator handle every draw, freed before this method
  # returns via Hegel::TestCase#with_text_generator, rather than
  # cached on this generator instance: hegel_string_generator_free
  # takes the context, so the handle cannot outlive it, and this
  # generator object can be drawn from again in a later run against a
  # different context. Caching the handle here would free it under
  # the wrong run's context the second time. hegel-rust's own text()
  # can cache its handle in a OnceLock because Rust's Drop runs the
  # free at a fixed, known scope exit; Ruby has no equivalent
  # lifetime to hang a cache on, so this pays the alphabet-building
  # cost every draw instead.
  tc.with_text_generator(
    min_size: @min_size, max_size: max_size, codec: @codec,
    min_codepoint: @min_codepoint || 0, max_codepoint: @max_codepoint || 0xFFFFFFFF
  ) { |generator| tc.generate_string(generator) }
end