Haskell:未经请求的unicode字符在i / o中转义

时间:2017-12-16 05:28:19

标签: haskell unicode

基本上我有一个用于复制文件的CLI实用程序。

{-# LANGUAGE OverloadedStrings #-}
...
import Turtle
...
{- Command line parser -}
-- | Represents command line options.
data Settings = Settings
  { sVerbose           :: Bool
...
  , sSrc               :: FilePath
  , sDst               :: FilePath
  }
...

跟踪代码:

...
-- | Extracts String From FilePath (unsafe and unofficial).
-- No double quotes allowed in paths.
strp :: FilePath -> String
strp path =
  let parts = splitOn "\"" (show path)
 in  parts !! 1
...
  putStrLn "Наблюдаем юникод"
  putStrLn $ strp (sSrc args)
  putStrLn $ strp src
...

工作代码:

...
src         <- realpath (sSrc args)
...

sSrc的控制台输入实际上是.

控制台输出:

Наблюдаем юникод
./
/home/alexey/common/Downloads/UpDown/Books/Audio/_Nonfiction_/Moral Combat \8211 Good and Evil in World War II [Unabridged]/
 1/26 /home/alexey/dir-dst/Moral Combat \\8211 Good and Evil in World War II [Unabridged]/01-Moral Combat \\8211 Part 01.mp3

\8211是某种破折号。转义路径由realpath .生成。我不知道原因。它是特定的i / o库吗?是编译选项吗?到目前为止唯一不能逃脱unicode字符的是putStrLn

我希望原始路径完好。

UPD:

Make it easy to extract a file path as Text from a FilePath

黑客现在看起来更漂亮:

import qualified Filesystem.Path.CurrentOS as FPS
import Data.Either.Extra
...
-- | Extracts String From FilePath
-- (good until deprecated system-filepath removed).
strp :: FilePath -> String
strp path = T.unpack $ fromRight "" (FPS.toText path)

它暂时有效。尽管如此,我仍然喜欢强制逃避的想法。 showprint通常非常有用,并且通常通过转义无用。没办法把它关掉?

1 个答案:

答案 0 :(得分:0)

子字符串showlet parts = splitOn "\"" (show path)realpath)而不是show生成。可能你应该删除对{{1}}的调用,尽管它并不是100%清楚你想要的跟踪代码。