hark-0.2: src/Helpers/ByteString.hs
{-----------------------------------------------------------------
(c) 2008-2009 Markus Dittrich
This program is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public
License Version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License Version 3 for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
--------------------------------------------------------------------}
-- | this module contains some helper function for ByteString
-- related functionality
module Helpers.ByteString ( andStr
, colonW
, dashW
, exclMarkW
, globW
, newlineW
, newline
, periodW
, plusW
, questMark
, slashW
, spaceCharW
, underscoreW
, binStr
, closeBracket
, dash
, docStr
, equalSign
, etcStr
, exclMark
, glob
, greater
, gr_eq
, includeStr
, libStr
, lib64Str
, low_eq
, orSymbol
, openBracket
, plus
, sbinStr
, shareStr
, smaller
, spaceChar
, tilde
, usrStr
, char_to_Word8
, remove_last_newline
, tokenize_BS
) where
-- imports
import Char(ord)
import qualified Data.ByteString as B(ByteString, init, last, split)
import qualified Data.ByteString.Char8 as BC(pack)
import Prelude
import Word(Word8)
--
-- some useful constants
--
-- Word8
colonW :: Word8
colonW = char_to_Word8 ':'
dashW :: Word8
dashW = char_to_Word8 '-'
exclMarkW :: Word8
exclMarkW = char_to_Word8 '!'
globW :: Word8
globW = char_to_Word8 '*'
newlineW :: Word8
newlineW = char_to_Word8 '\n'
periodW :: Word8
periodW = char_to_Word8 '.'
plusW :: Word8
plusW = char_to_Word8 '+'
spaceCharW :: Word8
spaceCharW = char_to_Word8 ' '
slashW :: Word8
slashW = char_to_Word8 '/'
underscoreW :: Word8
underscoreW = char_to_Word8 '_'
-- ByteString
andStr :: B.ByteString
andStr = BC.pack "&"
binStr :: B.ByteString
binStr = BC.pack "bin"
closeBracket :: B.ByteString
closeBracket = BC.pack ")"
dash :: B.ByteString
dash = BC.pack "-"
docStr :: B.ByteString
docStr = BC.pack "doc"
equalSign :: B.ByteString
equalSign = BC.pack "="
etcStr :: B.ByteString
etcStr = BC.pack "etc"
exclMark :: B.ByteString
exclMark = BC.pack "!"
glob :: B.ByteString
glob = BC.pack "*"
greater :: B.ByteString
greater = BC.pack ">"
gr_eq :: B.ByteString
gr_eq = BC.pack ">="
includeStr :: B.ByteString
includeStr = BC.pack "include"
libStr :: B.ByteString
libStr = BC.pack "lib"
lib64Str :: B.ByteString
lib64Str = BC.pack "lib64"
low_eq :: B.ByteString
low_eq = BC.pack "<="
newline :: B.ByteString
newline = BC.pack "\n"
orSymbol :: B.ByteString
orSymbol = BC.pack "||" -- "||"
openBracket :: B.ByteString
openBracket = BC.pack "("
plus :: B.ByteString
plus = BC.pack "+"
questMark :: B.ByteString
questMark = BC.pack "?"
sbinStr :: B.ByteString
sbinStr = BC.pack "sbin"
shareStr :: B.ByteString
shareStr = BC.pack "share"
smaller :: B.ByteString
smaller = BC.pack "<"
spaceChar :: B.ByteString
spaceChar = BC.pack " " -- " "
tilde :: B.ByteString
tilde = BC.pack "~"
usrStr :: B.ByteString
usrStr = BC.pack "usr"
-- | convert char into Word8
char_to_Word8 :: Char -> Word8
char_to_Word8 = fromIntegral . ord
-- | if the last element in a ByteString is a newline,
-- discard it, otherwise splitting the ByteString by
-- newlines will result in an empty last element
remove_last_newline :: B.ByteString -> B.ByteString
remove_last_newline string = if ( B.last string == newlineW ) then
B.init string
else
string
-- | split ByteString into tokens separated by whitespace
-- (currently only spaces) making sure to remove any trailing
-- newlines
tokenize_BS :: B.ByteString -> [B.ByteString]
tokenize_BS = B.split spaceCharW . remove_last_newline