gpt4 book ai didi

haskell - 替换 ByteString 中的换行符

转载 作者:行者123 更新时间:2023-12-04 21:05:22 25 4
gpt4 key购买 nike

我想要一个接受 ByteString 并替换换行符的函数 \n\n\r用逗号,但想不出一个好的方法来做到这一点。

import qualified Data.ByteString as BS
import Data.Char (ord)
import Data.Word (Word8)

endlWord8 = fromIntegral $ ord '\n' :: Word8

replace :: BS.ByteString -> BS.ByteString

我想过用 BS.map但看不到如何,因为我无法在 Word8 上进行模式匹配的。另一种选择是 BS.split然后加入 Word8 逗号,但这听起来又慢又不雅。有任何想法吗?

最佳答案

使用 Data.ByteString.Char8摆脱讨厌的Word8 , Char否则你必须做的转换。根据 Data.ByteString.Char8 first sentence性能不应该改变。

另外使用 B.span而不是 B.split因为你也想更换 \n\r组合而不仅仅是 \n .

我自己(可能很笨拙)尝试这样做:

module Test where

import Data.Monoid ((<>))
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Builder as Build
import qualified Data.ByteString.Lazy as LB

eatNewline :: ByteString -> (Maybe Char, ByteString)
eatNewline string
| B.null string = (Nothing, string)
| B.head string == '\n' && B.null (B.tail string) = (Just ',', B.empty)
| B.head string == '\n' && B.head (B.tail string) /= '\r' = (Just ',', B.drop 1 string)
| B.head string == '\n' && B.head (B.tail string) == '\r' = (Just ',', B.drop 2 string)
| otherwise = (Nothing, string)

replaceNewlines :: ByteString -> ByteString
replaceNewlines = LB.toStrict . Build.toLazyByteString . go mempty
where
go :: Build.Builder -> ByteString -> Build.Builder
go builder string = let (chunk, rest) = B.span (/= '\n') string
(c, rest1) = eatNewline rest
maybeComma = maybe mempty Build.char8 c
in if B.null rest1 then
builder <> Build.byteString chunk <> maybeComma
else
go (builder <> Build.byteString chunk <> maybeComma) rest1

希望 mappendData.ByteString.Builder次数不是线性的 mappend已经用于它的一个操作数,否则,这里会有一个二次算法。

关于haskell - 替换 ByteString 中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47265308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com