summaryrefslogtreecommitdiff
path: root/lib/prism/debug.rb
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2024-02-23 14:43:20 -0500
committergit <[email protected]>2024-02-23 20:02:19 +0000
commitec6532b458b34e907a2474bab70642414bbb0d05 (patch)
tree38639f44e86397f4202ae5ebe74a0989e955c652 /lib/prism/debug.rb
parentce8531fed4c7295aee94d24124914441db578136 (diff)
[ruby/prism] Add some encoding debugging to make testing easier
https://github.com/ruby/prism/commit/0c042561c6
Diffstat (limited to 'lib/prism/debug.rb')
-rw-r--r--lib/prism/debug.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/prism/debug.rb b/lib/prism/debug.rb
index c888436e4d..553205a04b 100644
--- a/lib/prism/debug.rb
+++ b/lib/prism/debug.rb
@@ -202,5 +202,48 @@ module Prism
def self.newlines(source)
Prism.parse(source).source.offsets
end
+
+ # A wrapping around prism's internal encoding data structures. This is used
+ # for reflection and debugging purposes.
+ class Encoding
+ # The name of the encoding, that can be passed to Encoding.find.
+ attr_reader :name
+
+ # Initialize a new encoding with the given name and whether or not it is
+ # a multibyte encoding.
+ def initialize(name, multibyte)
+ @name = name
+ @multibyte = multibyte
+ end
+
+ # Whether or not the encoding is a multibyte encoding.
+ def multibyte?
+ @multibyte
+ end
+
+ # Returns the number of bytes of the first character in the source string,
+ # if it is valid for the encoding. Otherwise, returns 0.
+ def width(source)
+ Encoding._width(name, source)
+ end
+
+ # Returns true if the first character in the source string is a valid
+ # alphanumeric character for the encoding.
+ def alnum?(source)
+ Encoding._alnum?(name, source)
+ end
+
+ # Returns true if the first character in the source string is a valid
+ # alphabetic character for the encoding.
+ def alpha?(source)
+ Encoding._alpha?(name, source)
+ end
+
+ # Returns true if the first character in the source string is a valid
+ # uppercase character for the encoding.
+ def upper?(source)
+ Encoding._upper?(name, source)
+ end
+ end
end
end