第26章:高效字串處理
base——String [Char],bytestring——ASCII,test——Unicode字串
1.bytestring函式庫
- 處理二進位制資料
- vector函式庫中Vector表示這樣的型別
Char預設表示Unicode編碼的字元,所以用Word8來代替C語言裡的Char
--適合大量的連續資料處理 --Data.Vector.Mutable IOVector Word8 --Data.Vector.Unboxed Vector Word8 --Data.Vector Vector Word8
bytestring函式庫ByteString型別(也常稱strict ByteString)
import qualified Data.ByteString as BS data ByteString = PS {-# UNPACK #-} !(ForeignPtr Word8) -- payload {-# UNPACK #-} !Int -- offset {-# UNPACK #-} !Int -- length
+ByteString包含一個指向Word8的指標,一個偏移量和一個長度
+ForeignPtr可以方便讓外部C呼叫的指標型別
+偏移量和長度的資料可以用來實現快速的字串分割等操作
+函式庫中提供的各種操作函式:head,length,map,foldl,reverse等
Lazy ByteString
使用連續區域儲存資料的問題,拼接操作申請記憶體有可能失敗而不得不把整個資料複製到新區域,且拼接ByteString的操作需要複製整個陣列的資料導致效率低;另一方面,連結串列資料結構是分散在記憶體中,但拼接操作只是對指標的操作,所以效率高,但訪問效能又低下。——Data.ByteString.Lazy中的Lazy ByteString能兼顧這兩種資料結構的優點。
+Lazy ByteString基本思路是分段表示一個字串,每一個子段內部是Strict ByteString,子段間通過指標相連
data ByteString = Empty | Chunk {-# UNPACK #-} !S.ByteString ByteString
+Data.ByteString.Streaming 流處理 streaming
ByteString Builder
+局域性優化——通過改造程式使用的資料結構,使之充分利用CPU快取記憶體實現加速
--預設返回Chunk尺寸是32KB的Lazy ByteString hGetContents :: Handle -> IO ByteString readFile :: FilePath -> IO ByteString ...
+為了自適應Chunk尺寸,尺寸小時使用記憶體複製的方式進行拼接,否則自動轉換為使用指標相連 +Data.ByteString.Builder Builder型別
data BufferRange = BufferRange {-# UNPACK #-} !(Ptr Word8) -- First byte of range {-# UNPACK #-} !(Ptr Word8) -- First byte /after/ range data Buffer = Buffer {-# UNPACK #-} !(ForeignPtr Word8) {-# UNPACK #-} !BufferRange data BuildSignal a = Done {-# UNPACK #-} !(Ptr Word8) a | BufferFull {-# UNPACK #-} !Int {-# UNPACK #-} !(Ptr Word8) (BuildStep a) | InsertChunk {-# UNPACK #-} !(Ptr Word8) S.ByteString (BuildStep a) type BuildStep a = BufferRange -> IO (BuildSignal a) newtype Builder = Builder (forall r. BuildStep r -> BuildStep r)
+操作Builder型別的函式
2.text和utf8-string函式庫
- UTF8 UTF16 UTF32
基於UTF16的text函式庫,處理文字
data Text = Text {-# UNPACK #-} !A.Array -- payload (Word16 elements) {-# UNPACK #-} !Int -- offset (units of Word16, not Char) {-# UNPACK #-} !Int -- length (units of Word16, not Char)
在處理Text型別時,通過組合基於Char的操作,在底層操作時text函式庫會自動完成UTF16和UTF32的轉換
- text函式庫的模組結構和bytestring類似
- 處理UTF8編碼字串的 utf8-string函式庫
一個統一的型別類
class IsString a where fromString :: String -> a
3. mono-traversable 函式庫
- 為了抽象上面介紹的關於字串 ByteString、Text 型別
- Data.MonoTraversable
Element型別家族
type family Element mono
推薦使用這個模組去處理一些和底層資料型別無關的計算模組,使得編寫的演算法和底層容器資料型別無關
相關文章
- 字串處理字串
- [譯] Transducers: JavaScript 中高效的資料處理 Pipeline(第 18 部分)JavaScript
- Guava字串處理Joiner、SplitterGuava字串
- PHP 陣列 & 字串處理PHP陣列字串
- bat 批處理字串操作BAT字串
- 簡單的字串處理字串
- shell字串處理總結字串
- JavaScript常用的字串處理方法JavaScript字串
- 06.字元和字串處理字元字串
- 使用Excel高效處理資料Excel
- 實用處理字串的linux命令字串Linux
- 處理stdin輸入的字串指令字串
- Java入門教程四(字串處理)Java字串
- C語言之字串處理函式C語言字串函式
- 處理字串的方法都在這裡字串
- MySQL 動態字串處理詳解MySql字串
- Linux 使用 shell 指令碼處理字串Linux指令碼字串
- 字串和日期時間的處理字串
- Java基礎-處理json字串解析案例JavaJSON字串
- JavaScript字串和時間處理隨筆JavaScript字串
- C++中的字串編碼處理C++字串編碼
- 如何高效的處理陣列對映陣列
- Python武器庫 - 科研中常用的python字串處理 - 字串擴充Python字串
- 處理一串字串的關鍵字字串
- 精通Python自然語言處理 1 :字串操作Python自然語言處理字串
- Java 運算子詳解與字串處理技巧Java字串
- Laravel response 返回的值全部處理為字串Laravel字串
- 藍橋杯 航班時間 (字串處理)C字串
- MVC字串處理及MVC@RenderSection小計MVC字串
- Java SimpleDateFormat處理日期與字串的轉換JavaORM字串
- 字串處理,push pop路徑,組合命令字串
- 訊號處理板卡學習資料第524篇:基於XCZU15EG的FMC+ 高效能通用訊號處理板卡
- Golang語言包-字串處理strings和字串型別轉換strconvGolang字串型別
- SQL中的常用的字串處理函式大全SQL字串函式
- 一個專業處理字串的IDEA外掛字串Idea
- f-strings: Python字串處理的瑞士軍刀Python字串
- 民聽相理前成第處向aru
- Vaex助力高效處理大規模資料集
- 億萬級資料處理的高效解決方案