| 1 |
-- Copyright 2020 United States Government as represented by the Administrator |
| 2 |
-- of the National Aeronautics and Space Administration. All Rights Reserved. |
| 3 |
-- |
| 4 |
-- Disclaimers |
| 5 |
-- |
| 6 |
-- Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 |
-- not use this file except in compliance with the License. You may obtain a |
| 8 |
-- copy of the License at |
| 9 |
-- |
| 10 |
-- https://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
-- |
| 12 |
-- Unless required by applicable law or agreed to in writing, software |
| 13 |
-- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 |
-- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 |
-- License for the specific language governing permissions and limitations |
| 16 |
-- under the License. |
| 17 |
-- |
| 18 |
-- | Auxiliary functions for working with values of type 'String'. |
| 19 |
module Data.String.Extra |
| 20 |
( |
| 21 |
-- * Safe I/O |
| 22 |
safeReadFile |
| 23 |
|
| 24 |
-- * String transformation |
| 25 |
, pascalCase |
| 26 |
|
| 27 |
-- * String sanitization |
| 28 |
, sanitizeLCIdentifier |
| 29 |
, sanitizeUCIdentifier |
| 30 |
) |
| 31 |
where |
| 32 |
|
| 33 |
-- External imports |
| 34 |
import Control.Exception ( catch ) |
| 35 |
import Data.Char ( toLower, toUpper ) |
| 36 |
import System.IO.Error ( isDoesNotExistError ) |
| 37 |
|
| 38 |
-- * Safe I/O |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
-- This function could also be placed in System.IO. However, we decide to |
| 44 |
-- include it in this module for symmetry with an analogous function for |
| 45 |
-- ByteString, included in Data.ByteString.Extra. |
| 46 |
safeReadFile :: FilePath -> IO (Either String String) |
| 47 |
safeReadFile fp = |
| 48 |
catch (return <$> readFile fp) $ \e -> |
| 49 |
if isDoesNotExistError e |
| 50 |
then return $ Left $ strStringFileNotFound fp |
| 51 |
else return $ Left $ strStringCannotOpenFile fp |
| 52 |
|
| 53 |
-- ** Error messages |
| 54 |
|
| 55 |
|
| 56 |
strStringFileNotFound :: FilePath -> String |
| 57 |
strStringFileNotFound fp = "File not found: " ++ fp |
| 58 |
|
| 59 |
|
| 60 |
strStringCannotOpenFile :: FilePath -> String |
| 61 |
strStringCannotOpenFile fp = "Error opening file: " ++ fp |
| 62 |
|
| 63 |
-- * Transformation |
| 64 |
|
| 65 |
|
| 66 |
pascalCase :: String -> String |
| 67 |
pascalCase = concatMap capitalize . parts |
| 68 |
where |
| 69 |
capitalize (x:xs) = toUpper x : map toLower xs |
| 70 |
capitalize [] = [] |
| 71 |
|
| 72 |
parts :: String -> [String] |
| 73 |
parts "" = [] |
| 74 |
parts s = |
| 75 |
let (l, r) = break (== '_') s |
| 76 |
in l : case r of |
| 77 |
[] -> [] |
| 78 |
(_:rs) -> parts rs |
| 79 |
|
| 80 |
-- * Sanitization |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
sanitizeLCIdentifier :: String -> String |
| 87 |
sanitizeLCIdentifier = headToLower . map sanitizeCharacter |
| 88 |
where |
| 89 |
sanitizeCharacter '-' = '_' |
| 90 |
sanitizeCharacter x = x |
| 91 |
|
| 92 |
headToLower :: [Char] -> [Char] |
| 93 |
headToLower [] = [] |
| 94 |
headToLower (x:xs) = toLower x : xs |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
sanitizeUCIdentifier :: String -> String |
| 101 |
sanitizeUCIdentifier = headToUpper . map sanitizeCharacter |
| 102 |
where |
| 103 |
sanitizeCharacter '-' = '_' |
| 104 |
sanitizeCharacter x = x |
| 105 |
|
| 106 |
headToUpper :: [Char] -> [Char] |
| 107 |
headToUpper [] = [] |
| 108 |
headToUpper (x:xs) = toUpper x : xs |