ghc-lib-parser-9.14.1.20251220: The GHC API, decoupled from GHC versions

Safe Haskell | Ignore
Language | GHC2021

GHC.Data.OsPath

Contents

- OsPath initialisation and transformation
- Common utility functions

Synopsis

- type OsPath = OsString
- data OsString
- encodeUtf :: MonadThrow m => FilePath -> m OsPath
- decodeUtf :: MonadThrow m => OsPath -> m FilePath
- unsafeDecodeUtf :: HasCallStack => OsPath -> FilePath
- unsafeEncodeUtf :: HasCallStack => String -> OsString
- os :: String -> OsString
- (</>) :: OsPath -> OsPath -> OsPath
- (<.>) :: OsPath -> OsString -> OsPath

# OsPath initialisation and transformation

type OsPath = OsString #

Type representing filenames/pathnames.

This type doesn't add any guarantees over `OsString`.

data OsString #

Newtype representing short operating system specific strings.

Internally this is either `WindowsString` or `PosixString`, depending on the platform. Both use unpinned `ShortByteString` for efficiency.

The constructor is only exported via System.OsString.Internal.Types, since dealing with the internals isn't generally recommended, but supported in case you need to write platform specific code.

#### Instances

Instances details

NFData OsString |
Instance details Defined in System.OsString.Internal.Types Methods rnf :: OsString -> () #
Monoid OsString | "String-Concatenation" for `OsString`. This is not the same as `(</>)`.
Instance details Defined in System.OsString.Internal.Types Methods mempty :: OsString # mappend :: OsString -> OsString -> OsString # mconcat :: [OsString] -> OsString #
Semigroup OsString |
Instance details Defined in System.OsString.Internal.Types Methods (<>) :: OsString -> OsString -> OsString # sconcat :: NonEmpty OsString -> OsString # stimes :: Integral b => b -> OsString -> OsString #
Generic OsString |
Instance details Defined in System.OsString.Internal.Types Associated Types
type Rep OsString |
Instance details Defined in System.OsString.Internal.Types type Rep OsString = D1 ('MetaData "OsString" "System.OsString.Internal.Types" "os-string-2.0.2-inplace" 'True) (C1 ('MetaCons "OsString" 'PrefixI 'True) (S1 ('MetaSel ('Just "getOsString") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 PlatformString)))
Methods from :: OsString -> Rep OsString x # to :: Rep OsString x -> OsString #
Show OsString | On windows, decodes as UCS-2. On unix prints the raw bytes without decoding.
Instance details Defined in System.OsString.Internal.Types Methods showsPrec :: Int -> OsString -> ShowS # show :: OsString -> String # showList :: [OsString] -> ShowS #
Eq OsString | Byte equality of the internal representation.
Instance details Defined in System.OsString.Internal.Types Methods (==) :: OsString -> OsString -> Bool # (/=) :: OsString -> OsString -> Bool #
Ord OsString | Byte ordering of the internal representation.
Instance details Defined in System.OsString.Internal.Types Methods compare :: OsString -> OsString -> Ordering # (<) :: OsString -> OsString -> Bool # (<=) :: OsString -> OsString -> Bool # (>) :: OsString -> OsString -> Bool # (>=) :: OsString -> OsString -> Bool # max :: OsString -> OsString -> OsString # min :: OsString -> OsString -> OsString #
Lift OsString |
Instance details Defined in System.OsString.Internal.Types Methods lift :: Quote m => OsString -> m Exp # liftTyped :: forall (m :: Type -> Type). Quote m => OsString -> Code m OsString #
type Rep OsString |
Instance details Defined in System.OsString.Internal.Types type Rep OsString = D1 ('MetaData "OsString" "System.OsString.Internal.Types" "os-string-2.0.2-inplace" 'True) (C1 ('MetaCons "OsString" 'PrefixI 'True) (S1 ('MetaSel ('Just "getOsString") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 PlatformString)))

encodeUtf :: MonadThrow m => FilePath -> m OsPath #

Partial unicode friendly encoding.

On windows this encodes as UTF16-LE (strictly), which is a pretty good guess. On unix this encodes as UTF8 (strictly), which is a good guess.

Throws an `EncodingException` if encoding fails. If the input does not contain surrogate chars, you can use `unsafeEncodeUtf`.

decodeUtf :: MonadThrow m => OsPath -> m FilePath #

Partial unicode friendly decoding.

On windows this decodes as UTF16-LE (strictly), which is a pretty good guess. On unix this decodes as UTF8 (strictly), which is a good guess.

Throws a `EncodingException` if decoding fails.

unsafeDecodeUtf :: HasCallStack => OsPath -> FilePath #

Decode an `OsPath` to `FilePath`, throwing an `error` if decoding failed. Prefer `decodeUtf` and gracious error handling.

unsafeEncodeUtf :: HasCallStack => String -> OsString #

Unsafe unicode friendly encoding.

Like `encodeUtf`, except it crashes when the input contains surrogate chars. For sanitized input, this can be useful.

os :: String -> OsString #

Fallibly converts String to OsString. Only intended to be used on literals.

# Common utility functions

(</>) :: OsPath -> OsPath -> OsPath #

Combine two paths with a path separator. If the second path starts with a path separator or a drive letter, then it returns the second. The intention is that `readFile (dir `</>` file)` will access the same file as `setCurrentDirectory dir; readFile file`.

    Posix:   "/directory" </> "file.ext" == "/directory/file.ext"
    Windows: "/directory" </> "file.ext" == "/directory\\file.ext"
             "directory" </> "/file.ext" == "/file.ext"
    Valid x => (takeDirectory x </> takeFileName x) `equalFilePath` x

Combined:

    Posix:   "/" </> "test" == "/test"
    Posix:   "home" </> "bob" == "home/bob"
    Posix:   "x:" </> "foo" == "x:/foo"
    Windows: "C:\\foo" </> "bar" == "C:\\foo\\bar"
    Windows: "home" </> "bob" == "home\\bob"

Not combined:

    Posix:   "home" </> "/bob" == "/bob"
    Windows: "home" </> "C:\\bob" == "C:\\bob"

Not combined (tricky):

On Windows, if a filepath starts with a single slash, it is relative to the root of the current drive. In [1], this is (confusingly) referred to as an absolute path. The current behavior of `</>` is to never combine these forms.

    Windows: "home" </> "/bob" == "/bob"
    Windows: "home" </> "\\bob" == "\\bob"
    Windows: "C:\\home" </> "\\bob" == "\\bob"

On Windows, from [1]: "If a file name begins with only a disk designator but not the backslash after the colon, it is interpreted as a relative path to the current directory on the drive with the specified letter." The current behavior of `</>` is to never combine these forms.

    Windows: "D:\\foo" </> "C:bar" == "C:bar"
    Windows: "C:\\foo" </> "C:bar" == "C:bar"

(<.>) :: OsPath -> OsString -> OsPath #

Add an extension, even if there is already one there, equivalent to `addExtension`.

    "/directory/path" <.> "ext" == "/directory/path.ext"
    "/directory/path" <.> ".ext" == "/directory/path.ext"