Skip to content

Commit 090c7d8

Browse files
formatting and utf-8
1 parent 69cc8c7 commit 090c7d8

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

lib/code/format.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ def format_left_operation(operation, indent:)
434434

435435
Array(operation[:others]).each do |other|
436436
right = format_nested_statement(other[:statement], indent: indent)
437-
operator = other[:operator]
437+
operator = format_operator(other[:operator])
438438

439439
expression =
440440
if compact_operator?(operator)
@@ -532,7 +532,7 @@ def extract_string_concatenation_parts(statement)
532532
end
533533

534534
def format_right_operation(operation, indent:)
535-
operator = operation[:operator].to_s
535+
operator = format_operator(operation[:operator])
536536
left =
537537
if %w[if unless].include?(operator)
538538
format_modifier_left(operation[:left], indent: indent)
@@ -595,6 +595,17 @@ def compact_operator?(operator)
595595
%w[. :: &. .. ...].include?(operator)
596596
end
597597

598+
def format_operator(operator)
599+
case operator.to_s
600+
when "||"
601+
"or"
602+
when "&&"
603+
"and"
604+
else
605+
operator.to_s
606+
end
607+
end
608+
598609
def format_ternary(ternary, indent:)
599610
left = format_nested_statement(ternary[:left], indent: indent)
600611
middle = format_nested_statement(ternary[:middle], indent: indent)

lib/code/object/string.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ def code_size
157157

158158
def code_strip
159159
String.new(raw.strip)
160+
rescue ArgumentError, Encoding::CompatibilityError => e
161+
raise unless e.message.include?("invalid byte sequence")
162+
163+
String.new(sanitized_utf8_raw.strip)
160164
end
161165

162166
def code_split(value)
@@ -172,6 +176,15 @@ def code_split(value)
172176
def present?
173177
raw.present?
174178
end
179+
180+
private
181+
182+
def sanitized_utf8_raw
183+
raw
184+
.dup
185+
.force_encoding(::Encoding::UTF_8)
186+
.encode(::Encoding::UTF_8, invalid: :replace, undef: :replace)
187+
end
175188
end
176189
end
177190
end

spec/code/format_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
%w[100000 100_000],
1414
%w[1000000 1_000_000],
1515
%w[1.0000000001 1.000_000_000_1],
16+
["true || false", "true or false"],
17+
["true && false", "true and false"],
1618
["{a:1}", "{ a: 1 }"],
1719
["[1,2,3]", "[1, 2, 3]"],
1820
["[1, 2, 3].select { |n| n.even? }", "[1, 2, 3].select { |n| n.even? }"],
@@ -54,7 +56,7 @@
5456
],
5557
[
5658
"sections << Html.join([Html.p { Html.b { \"{index + 1}. {title}\" } }, Html.p { query } if query.presence, Html.p { Html.a(href: link || inline_url) { :source } } if (link || inline_url), Html.p { Html.a(href: inline_url) { Html.img(src: inline_url, alt: title) } }, Html.p { Html.a(href: attachment_url) { \"télécharger\" } }].compact)",
57-
"sections << Html.join(\n [\n Html.p { Html.b { \"{index + 1}. {title}\" } },\n Html.p { query } if query.presence,\n Html.p {\n Html.a(href: link || inline_url) { :source }\n } if (link || inline_url),\n Html.p {\n Html.a(href: inline_url) { Html.img(src: inline_url, alt: title) }\n },\n Html.p {\n Html.a(href: attachment_url) { \"télécharger\" }\n }\n ].compact\n)"
59+
"sections << Html.join(\n [\n Html.p { Html.b { \"{index + 1}. {title}\" } },\n Html.p { query } if query.presence,\n Html.p {\n Html.a(href: link or inline_url) { :source }\n } if (link or inline_url),\n Html.p {\n Html.a(href: inline_url) { Html.img(src: inline_url, alt: title) }\n },\n Html.p {\n Html.a(href: attachment_url) { \"télécharger\" }\n }\n ].compact\n)"
5860
],
5961
[
6062
"safe = post.present? and !post[:over_18] and post[:post_hint] == :image and post[:url].to_string.strip.presence and (post[:url].to_string.strip.ends_with?(\".jpg\") or post[:url].to_string.strip.ends_with?(\".jpeg\") or post[:url].to_string.strip.ends_with?(\".png\") or post[:url].to_string.strip.include?(\"i.redd.it\"))",

spec/code/object/string_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@
1515
expect(Code.evaluate(input).to_s).to eq(expected)
1616
end
1717
end
18+
19+
describe "#code_strip" do
20+
it "replaces invalid utf-8 bytes instead of raising" do
21+
string = described_class.new("\xC3 ".b.force_encoding(Encoding::UTF_8))
22+
23+
expect(string.code_strip.to_s).to eq("\uFFFD")
24+
end
25+
end
1826
end

0 commit comments

Comments
 (0)