Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Created December 28, 2017 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-martin/51e78452a0be6fbf688c33a9a318f24e to your computer and use it in GitHub Desktop.
Save chris-martin/51e78452a0be6fbf688c33a9a318f24e to your computer and use it in GitHub Desktop.
-- | A string we're looking for in the source.
--
-- /Mnemonic: "needle in a haystack"/
newtype Needle = Needle Strict.Text
deriving Eq
-- | Needles are sorted by length with the longest needles first.
--
-- We use this ordering when performing replacements. For example, if both
-- "a" and "ab" are needles, then we give "ab" a chance to be found /before/
-- "a". Otherwise, "a" would always match first and the "ab" replacement
-- would never occur.
instance Ord Needle
where
compare = compare `on` \(Needle x) -> (negate (StrictText.length x), x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment