Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove quadratic limitation on counting quotes #5599

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions parser-typechecker/src/Unison/Syntax/TermPrinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,15 @@ pretty0
-- we only use this syntax if we're not wrapped in something else,
-- to avoid possible round trip issues if the text ends at an odd column
useRaw _ | p >= Annotation = Nothing
useRaw s | Text.find (== '\n') s == Just '\n' && Text.all ok s = n 3
useRaw s | Text.elem '\n' s && Text.all ok s = Just quotes
useRaw _ = Nothing
ok ch = isPrint ch || ch == '\n' || ch == '\r'
-- Picks smallest number of surrounding """ to be unique
n 10 = Nothing -- bail at 10, avoiding quadratic behavior in weird cases
n cur =
if null (Text.breakOnAll quotes s)
then Just quotes
else n (cur + 1)
where
quotes = Text.pack (replicate cur '"')
quotes = Text.pack (replicate numQuotes '"')
numQuotes = max 3 $ longestRun '"' s + 1
longestRun :: Char -> Text -> Int
longestRun c = maximum . (0 :) . map Text.length . filter ((== c) . Text.head) . Text.group

Text' s -> pure . fmt S.TextLiteral $ l $ U.ushow s
Char' c -> pure
. fmt S.CharLiteral
Expand Down
Loading