Data.ByteString.Lazy.Internal.ByteString为字符串? [英] Data.ByteString.Lazy.Internal.ByteString to string?

查看:114
本文介绍了Data.ByteString.Lazy.Internal.ByteString为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图写一个模块,它返回我的电脑的外部IP地址。
使用 Network.Wreq 获取函数,然后应用一个lense来获得 responseBody ,类型I最终是 Data.ByteString.Lazy.Internal.ByteString 。因为我想过滤掉结果体的尾部\\\
,我想随后使用它作为正则表达式。
问题:看似非常具体的ByteString类型不被正则表达式库接受,我没有办法将它转换为 String

$ b $ ; b

下面是我愚蠢的尝试,到目前为止(未编译)

  { - #语言OverloadedStrings# - } 

模块ExtIp(getExtIp),其中
进口Network.Wreq
进口Control.Lens
进口Data.BytesString.Lazy
进口Text.Regex .Posix

getExtIp :: IO String
getExtIp = do
r< - gethttp://myexternalip.com/raw
let body = r ^ 。 responseBody
let addr = body =〜[^ \\\
] * \\\

return(addr)

所以我的问题很明显:如何将那个有趣的特殊ByteString转换为 String ?解释我如何能够自己处理这样的问题,我也很感激。我尝试使用 unpack toString ,但不知道要导入哪些函数才能获得这些函数。



作为一个非常零星的haskell用户,我也怀疑是否有人能够向我展示定义这种函数的惯用的haskell方法。毕竟,我在这里展示的版本并没有考虑到可能出现的运行时错误/异常。

我根本不明白为什么你当你已经有一个 ByteString 时,坚持使用 String s,这是更快/更高效的实现。
导入 regex 几乎没有任何好处 - 解析ip地址我会使用 attoparsec ByteString s。



这是一个不使用正则表达式但返回String的版本 - 笔记我做了

  { - #LANGUAGE OverloadedStrings# - } $ b $不编译它,因为我没有安装haskell。 b 
模块ExtIp(getExtIp)其中
进口Network.Wreq
进口Control.Lens
进口Data.ByteString.Lazy.Char8作为CHAR8
进口Data.Char( isSpace)

getExtIp :: IO字符串
getExtIp = do
r< - gethttp://myexternalip.com/raw
return $ Char8.unpack $ trim(r ^。responseBody)
其中trim = Char8.reverse。 (Char8.dropWhile isSpace)。 Char8.reverse。 (Char8.dropWhile isSpace)


Trying to write a module which returns the external IP address of my computer. Using Network.Wreq get function, then applying a lense to obtain responseBody, the type I end up with is Data.ByteString.Lazy.Internal.ByteString. As I want to filter out the trailing "\n" of the result body, I want to use this for a regular expression subsequently. Problem: That seemingly very specific ByteString type is not accepted by regex library and I found no way to convert it to a String.

Here is my feeble attempt so far (not compiling).

{-# LANGUAGE OverloadedStrings #-}

module ExtIp (getExtIp) where
import Network.Wreq
import Control.Lens
import Data.BytesString.Lazy
import Text.Regex.Posix

getExtIp :: IO String
getExtIp = do
    r <- get "http://myexternalip.com/raw"
    let body = r ^. responseBody
    let addr = body =~ "[^\n]*\n"
    return (addr)

So my question is obviously: How to convert that funny special ByteString to a String? Explaining how I can approach such a problem myself is also appreciated. I tried to use unpack and toString but have no idea what to import to get those functions if they exist.

Being a very sporadic haskell user, I also wonder if someone could show me the idiomatic haskell way of defining such a function. The version I show here does not account for possible runtime errors/exceptions, after all.

解决方案

I simply do not understand why you insist on using Strings, when you have already a ByteString at hand that is the faster/more efficient implementation. Importing regex gives you almost no benefit - for parsing an ip-address I would use attoparsec which works great with ByteStrings.

Here is a version that does not use regex but returns a String - note I did not compile it for I have no haskell setup where I am right now.

{-# LANGUAGE OverloadedStrings #-}

module ExtIp (getExtIp) where
import Network.Wreq
import Control.Lens
import Data.ByteString.Lazy.Char8 as Char8
import Data.Char (isSpace)

getExtIp :: IO String
getExtIp = do
    r <- get "http://myexternalip.com/raw"
    return $ Char8.unpack $ trim (r ^. responseBody)
  where trim = Char8.reverse . (Char8.dropWhile isSpace) . Char8.reverse . (Char8.dropWhile isSpace)

这篇关于Data.ByteString.Lazy.Internal.ByteString为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆