Skip to content

Instantly share code, notes, and snippets.

@ryo1kato
Created September 2, 2014 16:50
Show Gist options
  • Select an option

  • Save ryo1kato/2ee21d3eec571f059780 to your computer and use it in GitHub Desktop.

Select an option

Save ryo1kato/2ee21d3eec571f059780 to your computer and use it in GitHub Desktop.
dropLast n bstr = BS.take (BS.length bstr - n) bstr
takeLast n bstr = BS.drop (BS.length bstr - n) bstr
reComp pat = makeRegexOpts compBlank execBlank pat
breakNextRegex re bstr =
if pos >= 0
then BS.splitAt pos bstr
else (bstr, BS.empty)
where (pos, len) = bstr =~ re :: (MatchOffset,MatchLength)
afterLast :: ByteStr -> ByteStr -> ByteStr
afterLast pat bstr = revSearch 0 bstr
where
revSearch n s
| BS.null s = bstr -- not found
| pat `BS.isSuffixOf` s = BS.drop (BS.length bstr - n) bstr
| otherwise = revSearch (n+1) (BSUS.unsafeInit s)
afterLastRegex :: String -> ByteStr -> ByteStr
afterLastRegex re bstr = revSearchRE 0 bstr
where
revSearchRE n s
| BS.null s = bstr
| offset >= 0 = takeLast (consumed - offset) bstr
| otherwise = revSearchRE consumed remaining
where
lastline = afterLast (pack "\n") s
(offset, l) = lastline =~ re :: (MatchOffset,MatchLength)
consumed = n + BS.length lastline
remaining = dropLast consumed bstr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment