Skip to content

Commit

Permalink
lib/string_ext.rb: add unicode_escape
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Li <[email protected]>
  • Loading branch information
rli9 committed Jul 24, 2024
1 parent be0bf6d commit c166d9e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/string_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ def numeric?
def uncolorize
gsub(/\e\[(\d+)?(;\d+)?(;\d+)?m/, '').gsub(/\e\[K/, '')
end

def unicode_escape
str = chars.map do |char|
if char.ascii_only?
char
else
char.codepoints.map { |cp| "\\u#{cp.to_s(16).rjust(4, '0')}" }.join
end
end

str.join
end
end

0 comments on commit c166d9e

Please sign in to comment.