summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-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