yi-rope-0.7.0.1/0000755000000000000000000000000012477171141011434 5ustar0000000000000000yi-rope-0.7.0.1/LICENSE0000644000000000000000000004317312477171141012451 0ustar0000000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. yi-rope-0.7.0.1/yi-rope.cabal0000644000000000000000000000301112477171141013777 0ustar0000000000000000name: yi-rope version: 0.7.0.1 synopsis: A rope data structure used by Yi description: A rope data structure used by Yi license: GPL-2 license-file: LICENSE author: Mateusz Kowalczyk maintainer: fuuzetsu@fuuzetsu.co.uk category: Yi build-type: Simple cabal-version: >=1.10 library if true ghc-options: -fpedantic-bottoms -fexpose-all-unfoldings -Wall -O2 if impl(ghc >= 7.8.1) ghc-options: -flate-dmd-anal exposed-modules: Yi.Rope build-depends: base >=4.5 && <5 , binary , bytestring , charsetdetect-ae >= 1.0.1 , data-default , deepseq , fingertree , text , text-icu hs-source-dirs: src default-language: Haskell2010 test-suite spec type: exitcode-stdio-1.0 default-language: Haskell2010 main-is: Spec.hs hs-source-dirs: test ghc-options: -funbox-strict-fields -Wall -O2 other-modules: Yi.RopeSpec build-depends: base , hspec , QuickCheck == 2.* , quickcheck-instances , text , yi-rope benchmark bench type: exitcode-stdio-1.0 default-language: Haskell2010 main-is: MainBenchmarkSuite.hs hs-source-dirs: bench ghc-options: -funbox-strict-fields -Wall -O2 build-depends: base >=4.5 && <5 , criterion , deepseq , yi-rope source-repository head type: git location: https://github.com/yi-editor/yi-rope.git yi-rope-0.7.0.1/Setup.hs0000644000000000000000000000005612477171141013071 0ustar0000000000000000import Distribution.Simple main = defaultMain yi-rope-0.7.0.1/bench/0000755000000000000000000000000012477171141012513 5ustar0000000000000000yi-rope-0.7.0.1/bench/MainBenchmarkSuite.hs0000644000000000000000000001367512477171141016574 0ustar0000000000000000{-# OPTIONS_GHC -fno-warn-orphans #-} {-# LANGUAGE OverloadedStrings #-} module Main where import Data.Char (isSpace) import Control.DeepSeq import Criterion.Main import qualified Criterion.Main as C import Data.Text (unlines, Text, replicate) import Prelude hiding (unlines) import qualified Yi.Rope as F longText :: Text longText = force . Data.Text.unlines $ Prelude.replicate 1000 "Lorem Спасибопожалусто dolor 中文測試 amet" {-# NOINLINE longText #-} longTextTree :: F.YiString longTextTree = force . F.fromText . Data.Text.unlines $ Prelude.replicate 1000 "Lorem Спасибопожалусто dolor 中文測試 amet" {-# NOINLINE longTextTree #-} longFRope :: F.YiString longFRope = force (F.fromText longText) {-# NOINLINE longFRope #-} wideText :: Text wideText = force . unlines $ Prelude.replicate 10 $ Data.Text.replicate 100 "Lorem Спасибопожалусто dolor 中文測試 amet " {-# NOINLINE wideText #-} shortText :: Text shortText = force . unlines $ Prelude.replicate 3 "Lorem Спасибопожалусто dolor 中文測試 amet" {-# NOINLINE shortText #-} tinyText :: Text tinyText = force $ "Lorem Спасибопожалусто dolor 中文測試 amet" {-# NOINLINE tinyText #-} wideFRope :: F.YiString wideFRope = force (F.fromText wideText) {-# NOINLINE wideFRope #-} benchOnText :: NFData b => a -> String -> (a -> b) -> Benchmark benchOnText text name f = C.bench name $ C.nf f text benchSplitAt :: NFData a => a -> String -> (Int -> a -> (a, a)) -> C.Benchmark benchSplitAt text name f = C.bench name $ C.nf (\x -> Prelude.foldr ((fst .) . f) x [1000, 999 .. 1]) text benchTakeDrop :: NFData a => a -> String -> (Int -> a -> a) -> C.Benchmark benchTakeDrop text name f = C.bench name $ C.nf (\x -> foldr f x [1000, 999 .. 1]) text -- | Chunk sizes to test with. chunkSizes :: [Int] chunkSizes = [1200] wideTexts :: (Int -> String, [(Int, F.YiString)]) wideTexts = (\x -> "wide " ++ show x, mkTextSample wideText) longTexts :: (Int -> String, [(Int, F.YiString)]) longTexts = (\x -> "long " ++ show x, mkTextSample longText) shortTexts :: (Int -> [Char], [(Int, F.YiString)]) shortTexts = (\x -> "short " ++ show x, mkTextSample shortText) tinyTexts :: (Int -> String, [(Int, F.YiString)]) tinyTexts = (\x -> "tiny " ++ show x, mkTextSample tinyText) mkTextSample :: Text -> [(Int, F.YiString)] mkTextSample s = force $ zipWith mkTexts chunkSizes (Prelude.repeat s) where mkTexts :: Int -> Text -> (Int, F.YiString) mkTexts x t = (x, F.fromText' x t) allTexts :: [(Int -> String, [(Int, F.YiString)])] allTexts = [longTexts {-, wideTexts, shortTexts, tinyTexts -}] allChars :: [(Int -> String, [(Int, Char)])] allChars = map mkChar "λa" where mkChar c = (\x -> unwords [ "char", [c], show x ], [(1, c)]) -- | Sample usage: -- -- > mkGroup "drop" F.drop allTexts benchOnText mkGroup :: String -- ^ Group name -> f -- ^ Function being benchmarked -> [(chsize -> String, [(chsize, input)])] -> (input -> String -> f -> Benchmark) -> Benchmark mkGroup n f subs r = bgroup n tests where mkTest s (l, t) = r t (s l) f tests = Prelude.concat $ map (\(s, t) -> map (mkTest s) t) subs onTextGroup :: NFData a => String -> (F.YiString -> a) -> Benchmark onTextGroup n f = mkGroup n f allTexts benchOnText onCharGroup :: NFData a => String -> (Char -> a) -> Benchmark onCharGroup n f = mkGroup n f allChars benchOnText onIntGroup :: String -> (Int -> F.YiString -> F.YiString) -> Benchmark onIntGroup n f = mkGroup n f allTexts benchTakeDrop onSplitGroup :: String -> (Int -> F.YiString -> (F.YiString, F.YiString)) -> Benchmark onSplitGroup n f = mkGroup n f allTexts benchSplitAt splitBench :: [Benchmark] splitBench = [ onTextGroup "split none" (F.split (== '×')) , onTextGroup "split lots" (F.split (\x -> x == 'a' || x == 'o')) , onTextGroup "split all" (F.split (const True)) ] wordsBench :: [Benchmark] wordsBench = -- The replicate here inflates the benchmark like mad, should be -- moved out. [ onTextGroup "unwords" (\x -> F.unwords (Prelude.replicate 100 x)) , onTextGroup "words" F.words ] spanBreakBench :: [Benchmark] spanBreakBench = [ onTextGroup "spanTrue" $ F.span (const True) , onTextGroup "spanFalse" $ F.span (const False) , onTextGroup "spanSpace" $ F.span isSpace , onTextGroup "breakTrue" $ F.break (const True) , onTextGroup "breakFalse" $ F.break (const False) , onTextGroup "breakSpace" $ F.break isSpace ] foldBench :: [Benchmark] foldBench = [ onTextGroup "foldCount" $ F.foldl' (\x _ -> x + 1) (0 :: Integer) , onTextGroup "foldId" $ F.foldl' F.snoc F.empty , onTextGroup "foldReverse" $ F.foldl' (\x y -> F.cons y x) F.empty ] main :: IO () main = defaultMain $ [ onIntGroup "drop" F.drop , onIntGroup "take" F.take , onTextGroup "cons" (F.cons 'λ') , onTextGroup "snoc" (`F.snoc` 'λ') , onCharGroup "singleton" F.singleton , onTextGroup "countNewLines" F.countNewLines , onTextGroup "lines" F.lines , onSplitGroup "splitAt" F.splitAt , onSplitGroup "splitAtLine" F.splitAtLine , onTextGroup "toReverseString" F.toReverseString , onTextGroup "toReverseText" F.toReverseText , onTextGroup "toText" F.toText , onTextGroup "length" F.length , onTextGroup "reverse" F.reverse , onTextGroup "null" F.null , onTextGroup "empty" $ const F.empty , onTextGroup "append" (\x -> F.append x x) , onTextGroup "concat x100" $ F.concat . Prelude.replicate 100 , onTextGroup "any OK, (== '中')" $ F.any (== '中') , onTextGroup "any bad, (== '×')" $ F.any (== '×') , onTextGroup "all OK (/= '×')" $ F.all (== '×') , onTextGroup "all bad, (== '中')" $ F.all (== '中') , onTextGroup "init" F.init , onTextGroup "tail" F.tail , onTextGroup "replicate 50" (F.replicate 50) ] ++ splitBench ++ wordsBench ++ spanBreakBench ++ foldBench yi-rope-0.7.0.1/test/0000755000000000000000000000000012477171141012413 5ustar0000000000000000yi-rope-0.7.0.1/test/Spec.hs0000644000000000000000000000005412477171141013640 0ustar0000000000000000{-# OPTIONS_GHC -F -pgmF hspec-discover #-} yi-rope-0.7.0.1/test/Yi/0000755000000000000000000000000012477171141012774 5ustar0000000000000000yi-rope-0.7.0.1/test/Yi/RopeSpec.hs0000644000000000000000000001214712477171141015055 0ustar0000000000000000{-# LANGUAGE OverloadedStrings #-} module Yi.RopeSpec (main, spec) where import Control.Applicative import Data.Char (isUpper, toUpper, isSpace) import qualified Data.Text as T import Test.Hspec import Test.Hspec.QuickCheck import Test.QuickCheck.Instances () import qualified Yi.Rope as R main :: IO () main = hspec spec infix 1 `isLike` -- | Converts the input to R.YiString before comparing results. isLike :: (Show a, Eq a) => (R.YiString -> a) -> (T.Text -> a) -> T.Text -> Expectation f `isLike` g = \t -> (f . R.fromText) t `shouldBe` g t infix 1 `isLikeT` -- | Applies given function over underlying 'R.YiString'. isLikeT :: (R.YiString -> R.YiString) -> (T.Text -> T.Text) -> T.Text -> Expectation f `isLikeT` g = \t -> (R.toText . f . R.fromText) t `shouldBe` g t spec :: Spec spec = modifyMaxSize (const 1000) $ do describe "Working with YiString is just like working with Text" $ do prop "id ~ id" $ id `isLikeT` id prop "R.take ~ T.take" $ \i -> R.take i `isLikeT` T.take i prop "R.drop ~ T.drop" $ \i -> R.drop i `isLikeT` T.drop i prop "R.reverse ~ T.reverse" $ R.reverse `isLikeT` T.reverse prop "R.length ~ T.length" $ R.length `isLike` T.length prop "R.null ~ T.null" $ R.null `isLike` T.null prop "R.countNewLines ~ T.count \\n" $ R.countNewLines `isLike` T.count "\n" prop "R.empty ~ T.empty" $ R.toText R.empty `shouldBe` T.empty prop "fst . R.splitAt ~ fst . T.splitAt" $ \i -> fst . R.splitAt i `isLikeT` fst . T.splitAt i prop "snd . R.splitAt ~ snd . T.splitAt" $ \i -> snd . R.splitAt i `isLikeT` snd . T.splitAt i prop "R.append ~ T.append" $ \t -> R.append (R.fromText t) `isLikeT` T.append t prop "R.concat ~ T.concat" $ \s -> (R.toText . R.concat . map R.fromText) s `shouldBe` T.concat s prop "R.head ~ T.head" $ R.head `isLike` (\x -> if T.null x then Nothing else Just (T.head x)) prop "R.last ~ T.last" $ R.last `isLike` (\x -> if T.null x then Nothing else Just (T.last x)) prop "R.cons ~ T.cons" $ \c -> R.cons c `isLikeT` T.cons c prop "R.snoc ~ T.snoc" $ \c -> R.cons c `isLikeT` T.cons c prop "R.singleton ~ T.singleton" $ \c -> (R.toText . R.singleton) c `shouldBe` T.singleton c prop "\\p -> R.any p ~ T.any p $ const True" $ \t -> R.any (const True) (R.fromText t) `shouldBe` T.any (const True) t prop "\\p -> R.any p ~ T.any p $ const False" $ \t -> R.any (const False) (R.fromText t) `shouldBe` T.any (const False) t prop "\\f -> R.withText ~ f $ T.toTitle" $ R.withText T.toTitle `isLikeT` T.toTitle prop "\\p -> R.dropWhile p ~ T.dropWhile p $ isUpper" $ R.dropWhile isUpper `isLikeT` T.dropWhile isUpper prop "\\p -> R.takeWhile p ~ T.takeWhile p $ isUpper" $ R.takeWhile isUpper `isLikeT` T.takeWhile isUpper prop "R.compare ~ T.compare" $ \t t' -> compare (R.fromText t) (R.fromText t') `shouldBe` compare t t' prop "\\p -> R.filter p ~ T.filter p $ isUpper" $ R.filter isUpper `isLikeT` T.filter isUpper prop "\\f -> R.map f ~ T.map f $ toUpper" $ R.map toUpper `isLikeT` T.map toUpper prop "\\p -> R.dropWhileEnd p ~ T.dropWhileEnd p $ isSpace" $ R.dropWhileEnd isSpace `isLikeT` T.dropWhileEnd isSpace prop "\\p -> R.takeWhileEnd p ~ rev . T.takeWhile p . rev $ isSpace" $ R.takeWhileEnd isSpace `isLikeT` T.reverse . T.takeWhile isSpace . T.reverse prop "R.words ~ T.words" $ \t -> R.toText <$> R.words (R.fromText t) `shouldBe` T.words t prop "R.unwords ~ T.unwords" $ \t -> R.toText (R.unwords (R.fromText <$> t)) `shouldBe` T.unwords t prop "\\p -> R.split p ~ T.split p $ isUpper" $ \t -> R.toText <$> R.split isUpper (R.fromText t) `shouldBe` T.split isUpper t prop "non-empty s ⊢ R.init s ~ T.init s" $ \t -> let t' = t `T.snoc` 'a' -- ensure non-empty in (fmap R.toText . R.init . R.fromText) t' `shouldBe` (Just . T.init) t' prop "non-empty s ⊢ R.tail s ~ T.tail s" $ \t -> let t' = t `T.snoc` 'a' in (fmap R.toText . R.tail . R.fromText) t' `shouldBe` (Just . T.tail) t' prop "\\p -> R.span p ~ T.span p $ isUpper" $ \t -> let (f, s) `shouldBeT` y = (R.toText f, R.toText s) `shouldBe` y in (R.span isUpper . R.fromText) t `shouldBeT` T.span isUpper t prop "\\p -> R.break p ~ T.break p $ isUpper" $ \t -> let (f, s) `shouldBeT` y = (R.toText f, R.toText s) `shouldBe` y in (R.break isUpper . R.fromText) t `shouldBeT` T.break isUpper t prop "\\p -> R.foldl' p ~ T.foldl' p $ \\x _ -> x + 1" $ \t -> let f x _ = x + (1 :: Integer) in (R.foldl' f 0 . R.fromText) t `shouldBe` T.foldl' f 0 t prop "R.replicate ~ T.replicate" $ \n -> R.replicate n `isLikeT` T.replicate n prop "R.intercalate ~ T.intercalate" $ \t ts -> R.toText (R.intercalate (R.fromText t) (R.fromText <$> ts)) `shouldBe` T.intercalate t ts yi-rope-0.7.0.1/src/0000755000000000000000000000000012477171141012223 5ustar0000000000000000yi-rope-0.7.0.1/src/Yi/0000755000000000000000000000000012477171141012604 5ustar0000000000000000yi-rope-0.7.0.1/src/Yi/Rope.hs0000644000000000000000000007543612477171141014064 0ustar0000000000000000{-# LANGUAGE BangPatterns #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} {-# OPTIONS_HADDOCK show-extensions #-} -- | -- Module : Yi.Rope -- License : GPL-2 -- Maintainer : yi-devel@googlegroups.com -- Stability : experimental -- Portability : portable -- -- This module defines a @rope@ data structure for use in Yi. This -- specific implementation uses a fingertree over Text. -- -- In contrast to our old implementation, we can now reap all the -- benefits of Text: automatic unicode handling and blazing fast -- implementation on underlying strings. This frees us from a lot of -- book-keeping. We don't lose out on not using ByteString directly -- because the old implementation encoded it into UTF8 anyway, making -- it unsuitable for storing anything but text. module Yi.Rope ( Yi.Rope.YiString, -- * Conversions to YiString Yi.Rope.fromString, Yi.Rope.fromText, Yi.Rope.fromString', Yi.Rope.fromText', -- * Conversions from YiString Yi.Rope.toString, Yi.Rope.toReverseString, Yi.Rope.toText, Yi.Rope.toReverseText, -- * Functions over content Yi.Rope.null, Yi.Rope.empty, Yi.Rope.take, Yi.Rope.drop, Yi.Rope.length, Yi.Rope.reverse, Yi.Rope.countNewLines, Yi.Rope.lines, Yi.Rope.lines', Yi.Rope.unlines, Yi.Rope.splitAt, Yi.Rope.splitAtLine, Yi.Rope.cons, Yi.Rope.snoc, Yi.Rope.singleton, Yi.Rope.head, Yi.Rope.last, Yi.Rope.append, Yi.Rope.concat, Yi.Rope.any, Yi.Rope.all, Yi.Rope.dropWhile, Yi.Rope.takeWhile, Yi.Rope.dropWhileEnd, Yi.Rope.takeWhileEnd, Yi.Rope.intercalate, Yi.Rope.intersperse, Yi.Rope.filter, Yi.Rope.map, Yi.Rope.words, Yi.Rope.unwords, Yi.Rope.split, Yi.Rope.init, Yi.Rope.tail, Yi.Rope.span, Yi.Rope.break, Yi.Rope.foldl', Yi.Rope.replicate, Yi.Rope.replicateChar, -- * IO ConverterName, unCn, Yi.Rope.readFile, Yi.Rope.writeFile, Yi.Rope.writeFileUsingText, Yi.Rope.writeFileWithConverter, -- * Escape latches to underlying content. Note that these are safe -- to use but it does not mean they should. Yi.Rope.fromRope, Yi.Rope.withText, Yi.Rope.unsafeWithText ) where import Codec.Text.Detect (detectEncodingName) import Control.Applicative ((<$>)) import Control.DeepSeq import Data.Binary import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as BSL import Data.Char (isSpace) import Data.Default import qualified Data.FingerTree as T import Data.FingerTree hiding (null, empty, reverse, split) import qualified Data.List as L (foldl') import Data.Maybe import Data.Monoid import Data.String (IsString(..)) import qualified Data.Text as TX import qualified Data.Text.Encoding as TXE import Data.Text.ICU.Convert import qualified Data.Text.IO as TF (writeFile) import Data.Typeable import Prelude hiding (drop) -- | Used to cache the size of the strings. data Size = Indices { charIndex :: {-# UNPACK #-} !Int -- ^ How many characters under here? , lineIndex :: Int -- ^ How many lines under here? } deriving (Eq, Show, Typeable) -- | A chunk storing the string of the type it is indexed by. It -- caches the length of stored string. data YiChunk = Chunk { chunkSize :: {-# UNPACK #-} !Int , _fromChunk :: {-# UNPACK #-} !TX.Text } deriving (Show, Eq, Typeable) -- | Makes a chunk from a given string. We allow for an arbitrary -- length function here to allow us to bypass the calculation with -- 'const' in case the length is known ahead of time. In most cases, -- the use of this is -- -- > mkChunk 'TX.Text.length' someText mkChunk :: (TX.Text -> Int) -- ^ The length function to use. -> TX.Text -> YiChunk mkChunk l t = Chunk (l t) t -- | Transform the chunk content. It's vital that the transformation -- preserves the length of the content. overChunk :: (TX.Text -> TX.Text) -- ^ Length-preserving content transformation. -> YiChunk -> YiChunk overChunk f (Chunk l t) = Chunk l (f t) -- | Counts number of newlines in the given 'TX.Text'. countNl :: TX.Text -> Int countNl = TX.count "\n" instance Monoid Size where mempty = Indices 0 0 Indices c l `mappend` Indices c' l' = Indices (c + c') (l + l') instance Measured Size YiChunk where measure (Chunk l t) = Indices l (countNl t) -- | A 'YiString' is a 'FingerTree' with cached column and line counts -- over chunks of 'TX.Text'. newtype YiString = YiString { fromRope :: FingerTree Size YiChunk } deriving (Show, Typeable) -- | Two 'YiString's are equal if their underlying text is. -- -- Implementation note: This just uses 'TX.Text' equality as there is -- no real opportunity for optimisation here except for a cached -- length check first. We could unroll the trees and mess around with -- matching prefixes but the overhead would be higher than a simple -- conversion and relying on GHC optimisation. -- -- The derived Eq implementation for the underlying tree only passes -- the equality check if the chunks are the same too which is not what -- we want. instance Eq YiString where t == t' = Yi.Rope.length t == Yi.Rope.length t' && toText t == toText t' instance NFData Size where rnf (Indices !c !l) = c `seq` l `seq` () instance NFData YiChunk where rnf (Chunk !i !t) = i `seq` rnf t instance NFData YiString where rnf = rnf . toText instance IsString YiString where fromString = Yi.Rope.fromString instance Monoid YiString where mempty = Yi.Rope.empty mappend = Yi.Rope.append mconcat = Yi.Rope.concat instance Ord YiString where compare x y = toText x `compare` toText y instance Default YiString where def = mempty (-|) :: YiChunk -> FingerTree Size YiChunk -> FingerTree Size YiChunk b -| t | chunkSize b == 0 = t | otherwise = b <| t (|-) :: FingerTree Size YiChunk -> YiChunk -> FingerTree Size YiChunk t |- b | chunkSize b == 0 = t | otherwise = t |> b -- | Default size chunk to use. Currently @1200@ as this is what -- benchmarks suggest. -- -- This makes the biggest difference with 'lines'-like and -- 'concat'-like functions. Bigger chunks make 'concat' (much) faster -- but 'lines' slower. In general it seems that we benefit more from -- larger chunks and 1200 seems to be the sweet spot. defaultChunkSize :: Int defaultChunkSize = 1200 -- | Reverse the whole underlying string. -- -- This involves reversing the order of the chunks as well as content -- of the chunks. We use a little optimisation here that re-uses the -- content of each chunk but this exposes a potential problem: after -- many transformations, our chunks size might become quite varied -- (but never more than the default size), perhaps we should -- periodically rechunk the tree to recover nice sizes? reverse :: YiString -> YiString reverse = YiString . fmap' (overChunk TX.reverse) . T.reverse . fromRope -- | See 'fromText'. fromString :: String -> YiString fromString = fromText . TX.pack -- | See 'fromText''. fromString' :: Int -> String -> YiString fromString' n = fromText' n . TX.pack -- | See 'toText'. toString :: YiString -> String toString = TX.unpack . toText -- | See 'toReverseText'. -- -- Note that it is actually ~4.5 times faster to use 'toReverseText' -- and unpack the result than to convert to 'String' and use -- 'Prelude.reverse'. toReverseString :: YiString -> String toReverseString = TX.unpack . toReverseText -- | This is like 'fromText' but it allows the user to specify the -- chunk size to be used. Uses 'defaultChunkSize' if the given -- size is <= 0. fromText' :: Int -> TX.Text -> YiString fromText' n | n <= 0 = fromText' defaultChunkSize | otherwise = YiString . r T.empty . f where f = TX.chunksOf n -- Convert the given string into chunks in the tree. We have a -- special case for a single element case: because we split on -- predetermined chunk size, we know that all chunks but the last -- one will be the specified size so we can optimise here instead -- of having to recompute chunk size at creation. r :: FingerTree Size YiChunk -> [TX.Text] -> FingerTree Size YiChunk r !tr [] = tr r !tr (t:[]) = tr |- mkChunk TX.length t r !tr (t:ts) = let r' = tr |- mkChunk (const n) t in r r' ts -- | Converts a 'TX.Text' into a 'YiString' using -- 'defaultChunkSize'-sized chunks for the underlying tree. fromText :: TX.Text -> YiString fromText = fromText' defaultChunkSize -- | Consider whether you really need to use this! toText :: YiString -> TX.Text toText = TX.concat . go . fromRope where go :: FingerTree Size YiChunk -> [TX.Text] go t = case viewl t of Chunk _ !c :< cs -> c : go cs EmptyL -> [] -- | Spits out the underlying string, reversed. -- -- Note that this is actually slightly faster than manually unrolling -- the tree from the end, 'TX.reverse'ing each chunk and -- 'TX.concat'ing, at least with -O2 which you really need to be using -- with 'TX.Text' anyway. toReverseText :: YiString -> TX.Text toReverseText = TX.reverse . toText -- | Checks if the given 'YiString' is actually empty. null :: YiString -> Bool null = T.null . fromRope -- | Creates an empty 'YiString'. empty :: YiString empty = YiString T.empty -- | Length of the whole underlying string. -- -- Amortized constant time. length :: YiString -> Int length = charIndex . measure . fromRope -- | Count the number of newlines in the underlying string. This is -- actually amortized constant time as we cache this information in -- the underlying tree. countNewLines :: YiString -> Int countNewLines = lineIndex . measure . fromRope -- | Append two 'YiString's. -- -- We take the extra time to optimise this append for many small -- insertions. With naive append of the inner fingertree with 'T.><', -- it is often the case that we end up with a large collection of tiny -- chunks. This function instead tries to join the underlying trees at -- outermost chunks up to 'defaultChunkSize' which while slower, -- should improve memory usage. -- -- I suspect that this pays for itself as we'd spend more time -- computing over all the little chunks than few large ones anyway. append :: YiString -> YiString -> YiString append (YiString t) (YiString t') = case (viewr t, viewl t') of (EmptyR, _) -> YiString t' (_, EmptyL) -> YiString t (ts :> Chunk l x, Chunk l' x' :< ts') -> let len = l + l' in case compare len defaultChunkSize of GT -> YiString (t <> t') _ -> YiString (ts |- Chunk len (x <> x') <> ts') -- | Concat a list of 'YiString's. concat :: [YiString] -> YiString concat = L.foldl' append empty -- | Take the first character of the underlying string if possible. head :: YiString -> Maybe Char head (YiString t) = case viewl t of EmptyL -> Nothing Chunk _ x :< _ -> if TX.null x then Nothing else Just (TX.head x) -- | Take the last character of the underlying string if possible. last :: YiString -> Maybe Char last (YiString t) = case viewr t of EmptyR -> Nothing _ :> Chunk _ x -> if TX.null x then Nothing else Just (TX.last x) -- | Takes every character but the last one: returns Nothing on empty -- string. init :: YiString -> Maybe YiString init (YiString t) = case viewr t of EmptyR -> Nothing ts :> Chunk 0 _ -> Yi.Rope.init (YiString ts) ts :> Chunk l x -> Just . YiString $ ts |- Chunk (l - 1) (TX.init x) -- | Takes the tail of the underlying string. If the string is empty -- to begin with, returns Nothing. tail :: YiString -> Maybe YiString tail (YiString t) = case viewl t of EmptyL -> Nothing Chunk 0 _ :< ts -> Yi.Rope.tail (YiString ts) Chunk l x :< ts -> Just . YiString $ Chunk (l - 1) (TX.tail x) -| ts -- | Splits the string at given character position. -- -- If @position <= 0@ then the left string is empty and the right string -- contains everything else. -- -- If @position >= length of the string@ then the left string contains -- everything and the right string is empty. -- -- Implementation note: the way this works is by splitting the -- underlying finger at a closest chunk that goes *over* the given -- position (see 'T.split'). This either results in a perfect split at -- which point we're done or more commonly, it leaves as few -- characters short and we need to take few characters from the first -- chunk of the right side of the split. We do precisely that. -- -- All together, this split is only as expensive as underlying -- 'T.split', the cost of splitting a chunk into two, the cost of one -- cons and one cons of a chunk and lastly the cost of 'T.splitAt' of -- the underlying 'TX.Text'. It turns out to be fairly fast all -- together. splitAt :: Int -> YiString -> (YiString, YiString) splitAt n (YiString t) | n <= 0 = (mempty, YiString t) | otherwise = case viewl s of Chunk l x :< ts | n' /= 0 -> let (lx, rx) = TX.splitAt n' x in (YiString $ f |> Chunk n' lx, YiString $ Chunk (l - n') rx -| ts) _ -> (YiString f, YiString s) where (f, s) = T.split ((> n) . charIndex) t n' = n - charIndex (measure f) -- | Takes the first n given characters. take :: Int -> YiString -> YiString take 1 = maybe def Yi.Rope.singleton . Yi.Rope.head take n = fst . Yi.Rope.splitAt n -- | Drops the first n characters. drop :: Int -> YiString -> YiString drop 1 = fromMaybe def . Yi.Rope.tail drop n = snd . Yi.Rope.splitAt n -- | The usual 'Prelude.dropWhile' optimised for 'YiString's. dropWhile :: (Char -> Bool) -> YiString -> YiString dropWhile p = YiString . go . fromRope where go t = case viewl t of EmptyL -> T.empty Chunk 0 _ :< ts -> go ts Chunk l x :< ts -> let r = TX.dropWhile p x l' = TX.length r in case compare l' l of -- We dropped nothing so we must be done. EQ -> t -- We dropped something, if it was everything then drop from -- next chunk. LT | TX.null r -> go ts -- It wasn't everything and we have left-overs, we must be done. | otherwise -> Chunk l' r <| ts -- We shouldn't really get here or it would mean that -- dropping stuff resulted in more content than we had. This -- can only happen if unsafe functions don't preserve the -- chunk size and it goes out of sync with the text length. -- Preserve this abomination, it may be useful for -- debugging. _ -> Chunk l' r -| ts -- | As 'Yi.Rope.dropWhile' but drops from the end instead. dropWhileEnd :: (Char -> Bool) -> YiString -> YiString dropWhileEnd p = YiString . go . fromRope where go t = case viewr t of EmptyR -> T.empty ts :> Chunk 0 _ -> go ts ts :> Chunk l x -> let r = TX.dropWhileEnd p x l' = TX.length r in case compare l' l of EQ -> t LT | TX.null r -> go ts | otherwise -> ts |> Chunk l' r _ -> ts |- Chunk l' r -- | The usual 'Prelude.takeWhile' optimised for 'YiString's. takeWhile :: (Char -> Bool) -> YiString -> YiString takeWhile p = YiString . go . fromRope where go t = case viewl t of EmptyL -> T.empty Chunk 0 _ :< ts -> go ts Chunk l x :< ts -> let r = TX.takeWhile p x l' = TX.length r in case compare l' l of -- We took the whole chunk, keep taking more. EQ -> Chunk l x -| go ts -- We took some stuff but not everything so we're done. -- Alternatively, we took more than the size chunk so -- preserve this wonder. This should only ever happen if you -- use unsafe functions and Chunk size goes out of sync with -- actual text length. _ -> T.singleton $ Chunk l' r -- | Like 'Yi.Rope.takeWhile' but takes from the end instead. takeWhileEnd :: (Char -> Bool) -> YiString -> YiString takeWhileEnd p = YiString . go . fromRope where go t = case viewr t of EmptyR -> T.empty ts :> Chunk 0 _ -> go ts ts :> Chunk l x -> case compare l' l of EQ -> go ts |> Chunk l x _ -> T.singleton $ Chunk l' r where -- no TX.takeWhileEnd – https://github.com/bos/text/issues/89 r = TX.reverse . TX.takeWhile p . TX.reverse $ x l' = TX.length r -- | Returns a pair whose first element is the longest prefix -- (possibly empty) of t of elements that satisfy p, and whose second -- is the remainder of the string. See also 'TX.span'. -- -- This implementation uses 'Yi.Rope.splitAt' which actually is just -- as fast as hand-unrolling the tree. GHC sure is great! span :: (Char -> Bool) -> YiString -> (YiString, YiString) span p y = let x = Yi.Rope.takeWhile p y in case Yi.Rope.splitAt (Yi.Rope.length x) y of -- Re-using ‘x’ seems to gain us a minor performance -- boost. (_, y') -> (x, y') -- | Just like 'Yi.Rope.span' but with the predicate negated. break :: (Char -> Bool) -> YiString -> (YiString, YiString) break p = Yi.Rope.span (not . p) -- | Concatenates the list of 'YiString's after inserting the -- user-provided 'YiString' between the elements. -- -- Empty 'YiString's are not ignored and will end up as strings of -- length 1. If you don't want this, it's up to you to pre-process the -- list. Just as with 'Yi.Rope.intersperse', it is up to the user to -- pre-process the list. intercalate :: YiString -> [YiString] -> YiString intercalate _ [] = mempty intercalate (YiString t') (YiString s:ss) = YiString $ s >< go ss where go [] = mempty go (YiString t : ts') = t' >< t >< go ts' -- | Intersperses the given character between the 'YiString's. This is -- useful when you have a bunch of strings you just want to separate -- something with, comma or a dash. Note that it only inserts the -- character between the elements. -- -- What's more, the result is a single 'YiString'. You can easily -- achieve a version that blindly inserts elements to the back by -- mapping over the list instead of using this function. -- -- You can think of it as a specialised version of -- 'Yi.Rope.intercalate'. Note that what this does __not__ do is -- intersperse characters into the underlying text, you should convert -- and use 'TX.intersperse' for that instead. intersperse :: Char -> [YiString] -> YiString intersperse _ [] = mempty intersperse c (t:ts) = t <> go ts where go [] = mempty go (t':ts') = (c `cons` t') <> go ts' -- | Add a 'Char' in front of a 'YiString'. -- -- We add the character to the front of the first chunk. This does -- mean that a lot of 'cons' might result in an abnormally large first -- chunk so if you have to do that, consider using 'append' instead.. cons :: Char -> YiString -> YiString cons c (YiString t) = case viewl t of EmptyL -> Yi.Rope.singleton c Chunk !l x :< ts -> YiString $ Chunk (l + 1) (c `TX.cons` x) <| ts -- | Add a 'Char' in the back of a 'YiString'. -- -- We add the character to the end of the last chunk. This does mean -- that a lot of 'snoc' might result in an abnormally large last chunk -- so if you have to do that, consider using 'append' instead.. snoc :: YiString -> Char -> YiString snoc (YiString t) c = case viewr t of EmptyR -> Yi.Rope.singleton c ts :> Chunk l x -> YiString $ ts |> Chunk (l + 1) (x `TX.snoc` c) -- | Single character 'YiString'. Consider whether it's worth creating -- this, maybe you can use 'cons' or 'snoc' instead? singleton :: Char -> YiString singleton c = YiString . T.singleton $ Chunk 1 (TX.singleton c) -- | Splits the underlying string before the given line number. -- Zero-indexed lines. -- -- Splitting at line <= 0 gives you an empty string. Splitting at -- @n > 0@ gives you the first n lines. -- -- Also see 'splitAtLine''. splitAtLine :: Int -> YiString -> (YiString, YiString) splitAtLine n r | n <= 0 = (empty, r) | otherwise = splitAtLine' (n - 1) r -- | Splits the underlying string after the given line number. -- Zero-indexed lines. -- -- Splitting at line <= 0 gives you the first line. Splitting at -- @n > 0@ gives you the first n + 1 lines. -- -- The implementation is similar to that of 'splitAt' except we are -- now looking for extra newlines in the next chunk rather than extra -- characters. splitAtLine' :: Int -> YiString -> (YiString, YiString) splitAtLine' p (YiString tr) = case viewl s of ch@(Chunk _ x) :< r -> let excess = lineIndex (measure f) + lineIndex (measure ch) - p - 1 (lx, rx) = cutExcess excess x in (YiString $ f |- mkChunk TX.length lx, YiString $ mkChunk TX.length rx -| r) _ -> (YiString f, YiString s) where (f, s) = T.split ((p <) . lineIndex) tr cutExcess :: Int -> TX.Text -> (TX.Text, TX.Text) cutExcess n t = case TX.length t of 0 -> (TX.empty, TX.empty) _ -> let ns = countNl t ls = TX.lines t front = TX.unlines $ Prelude.take (ns - n) ls back = TX.drop (TX.length front) t in if n >= ns then (t, TX.empty) else (front, back) -- | This is like 'lines'' but it does *not* preserve newlines. -- -- Specifically, we just strip the newlines from the result of -- 'lines''. -- -- This behaves slightly differently than the old split: the number of -- resulting strings here is equal to the number of newline characters -- in the underlying string. This is much more consistent than the old -- behaviour which blindly used @ByteString@s split and stitched the -- result back together which was inconsistent with the rest of the -- interface which worked with number of newlines. lines :: YiString -> [YiString] lines = Prelude.map dropNl . lines' where dropNl (YiString t) = case viewr t of EmptyR -> Yi.Rope.empty ts :> ch@(Chunk l tx) -> YiString $ ts |- if TX.null tx then ch else case TX.last tx of '\n' -> Chunk (l - 1) (TX.init tx) _ -> ch -- | Splits the 'YiString' into a list of 'YiString' each containing a -- line. -- -- Note that in old implementation this allowed an arbitrary character -- to split on. If you want to do that, manually convert 'toText' and -- use 'TX.splitOn' to suit your needs. This case is optimised for -- newlines only which seems to have been the only use of the original -- function. -- -- The newlines are preserved so this should hold: -- -- > 'toText' . 'concat' . 'lines'' ≡ 'toText' -- -- but the underlying structure might change: notably, chunks will -- most likely change sizes. lines' :: YiString -> [YiString] lines' t = let (YiString f, YiString s) = splitAtLine' 0 t in if T.null s then if T.null f then [] else [YiString f] else YiString f : lines' (YiString s) -- | Joins up lines by a newline character. It does not leave a -- newline after the last line. If you want a more classical -- 'Prelude.unlines' behaviour, use 'Yi.Rope.map' along with -- 'Yi.Rope.snoc'. unlines :: [YiString] -> YiString unlines = Yi.Rope.intersperse '\n' -- | 'YiString' specialised @any@. -- -- Implementation note: this currently just does any by doing ‘TX.Text’ -- conversions upon consecutive chunks. We should be able to speed it -- up by running it in parallel over multiple chunks. any :: (Char -> Bool) -> YiString -> Bool any p = go . fromRope where go x = case viewl x of EmptyL -> False Chunk _ t :< ts -> TX.any p t || go ts -- | 'YiString' specialised @all@. -- -- See the implementation note for 'Yi.Rope.any'. all :: (Char -> Bool) -> YiString -> Bool all p = go . fromRope where go x = case viewl x of EmptyL -> False Chunk _ t :< ts -> TX.all p t || go ts -- | To serialise a 'YiString', we turn it into a regular 'String' -- first. instance Binary YiString where put = put . toString get = Yi.Rope.fromString <$> get -- | 'ConverterName' is used to convey information about the -- underlying 'Converter' used within the buffer to encode and decode -- text. It is mostly here due to the lack of 'Binary' instance for -- 'Converter' itself. newtype ConverterName = CN String deriving (Show, Eq, Ord, Read, Typeable) -- | Returns the underlying string. unCn :: ConverterName -> String unCn (CN s) = s -- | Simply 'put's/'get's the underlying 'String'. instance Binary ConverterName where put (CN s) = put s get = CN <$> get -- | Writes the given 'YiString' to the given file, according to the -- 'Converter' specified by 'ConverterName'. You can obtain a -- 'ConverterName' through 'readFile'. If you have a 'Converter', use -- 'writeFileWithConverter' instead. -- -- If you don't care about the encoding used such as when creating a -- brand new file, you can use 'writeFileUsingText'. -- -- It's up to the user to handle exceptions. -- -- Returns an error message if conversion failed, otherwise Nothing -- on success. writeFile :: FilePath -> YiString -> ConverterName -> IO (Maybe TX.Text) writeFile f s (CN cn) = open cn (Just True) >>= writeFileWithConverter f s -- | As 'writeFile' but using the provided 'Converter' rather than -- creating one from a 'ConverterName'. -- -- It's up to the user to handle exceptions. writeFileWithConverter :: FilePath -> YiString -> Converter -> IO (Maybe TX.Text) writeFileWithConverter f s c = do let bytes = fromUnicode c $ toText s errorMsg = "Could not encode text with specified encoding" enc <- detectEncoding errorMsg $ BSL.fromChunks [bytes] case enc of Left err -> return $ Just err Right (_, (CN cn)) -> do if cn == getName c then BS.writeFile f bytes >> return Nothing else return . Just $ errorMsg -- | Write a 'YiString' into the given file. This function uses -- 'TF.writeFile' to do the writing: if you have special needs for -- preserving encoding/decoding, use 'writeFile' instead. -- -- It's up to the user to handle exceptions. writeFileUsingText :: FilePath -> YiString -> IO () writeFileUsingText f = TF.writeFile f . toText -- | Reads file into the rope, also returning the 'ConverterName' that -- was used for decoding. You should resupply this to 'writeFile' if -- you're aiming to preserve the original encoding. -- -- If we fail to guess the encoding used, error message is given -- instead. -- -- It is up to the user to handle exceptions not directly related to -- character decoding. readFile :: FilePath -> IO (Either TX.Text (YiString, ConverterName)) readFile fp = BSL.readFile fp >>= detectEncoding err where err = "Could not guess the encoding of " <> TX.pack fp -- | Detects the encoding of a sequence of bytes. -- -- Presumably the calculating the 'YiString' is lazy so it is fine -- to use this to only get the converter name. -- -- Also allows specification of the error to return if the encoding -- of the bytes cannot be detected. The error returns won't necessarily -- be this error - it is used only if no encoding name is detected at all. detectEncoding :: TX.Text -> BSL.ByteString -> IO (Either TX.Text (YiString, ConverterName)) detectEncoding err cs = case detectEncodingName cs of Nothing -> return $ case TXE.decodeUtf8' $ BSL.toStrict cs of -- The detection failed but stay optimistic and try as UTF8 anyway. Left _ -> Left err Right tx -> Right (fromText tx, CN "UTF-8") Just enc -> do let ke = if enc == "ASCII" then Just "UTF-8" else listToMaybe $ aliases enc case ke of Nothing -> return . Left . TX.pack $ "Don't know how to decode as " <> enc Just s -> do c <- open s (Just True) let st = BSL.toStrict cs return $ Right (fromText $ toUnicode c st, CN $ getName c) -- | Filters the characters from the underlying string. -- -- >>> filter (/= 'a') "bac" -- "bc" filter :: (Char -> Bool) -> YiString -> YiString filter p = YiString . go . fromRope where go t = case viewl t of EmptyL -> T.empty Chunk _ x :< ts -> mkChunk TX.length (TX.filter p x) -| go ts -- | Maps the characters over the underlying string. map :: (Char -> Char) -> YiString -> YiString map f = YiString . go . fromRope where go t = case viewl t of EmptyL -> T.empty Chunk l x :< ts -> Chunk l (TX.map f x) <| go ts -- | Join given 'YiString's with a space. Empty lines will be filtered -- out first. unwords :: [YiString] -> YiString unwords = Yi.Rope.intersperse ' ' -- | Splits the given 'YiString' into a list of words, where spaces -- are determined by 'isSpace'. No empty strings are in the result -- list. words :: YiString -> [YiString] words = Prelude.filter (not . Yi.Rope.null) . Yi.Rope.split isSpace -- | Splits the 'YiString' on characters matching the predicate, like -- 'TX.split'. -- -- For splitting on newlines use 'Yi.Rope.lines' or 'Yi.Rope.lines'' -- instead. -- -- Implementation note: GHC actually makes this naive implementation -- about as fast and in cases with lots of splits, faster, as a -- hand-rolled version on chunks with appends which is quite amazing -- in itself. split :: (Char -> Bool) -> YiString -> [YiString] split p = fmap fromText . TX.split p . toText -- | Left fold. -- -- Benchmarks show that folding is actually Pretty Damn Slow™: consider -- whether folding is really the best thing to use in your scenario. foldl' :: (a -> Char -> a) -> a -> YiString -> a foldl' f a = go a . fromRope where go acc t = case viewl t of EmptyL -> acc Chunk _ x :< ts -> let r = TX.foldl' f acc x in r `seq` go r ts -- | Replicate the given YiString set number of times, concatenating -- the results. Also see 'Yi.Rope.replicateChar'. replicate :: Int -> YiString -> YiString replicate n t | n <= 0 = mempty | otherwise = t <> Yi.Rope.replicate (n - 1) t -- | Replicate the given character set number of times and pack the -- result into a 'YiString'. -- -- >>> replicateChar 4 ' ' -- " " replicateChar :: Int -> Char -> YiString replicateChar n = fromText . TX.replicate n . TX.singleton -- | Helper function doing conversions of to and from underlying -- 'TX.Text'. You should aim to implement everything in terms of -- 'YiString' instead. -- -- Please note that this maps over each __chunk__ so this can only be -- used with layout-agnostic functions. For example -- -- >>> let t = 'fromString' "abc" <> 'fromString' "def" -- >>> 'toString' $ 'withText' 'TX.reverse' t -- "cbafed" -- -- Probably doesn't do what you wanted, but 'TX.toUpper' would. -- Specifically, for any @f : 'TX.Text' → 'TX.Text'@, 'withText' will -- only do the ‘expected’ thing iff -- -- @f x <> f y ≡ f (x <> y)@ -- -- which should look very familiar. withText :: (TX.Text -> TX.Text) -> YiString -> YiString withText f = YiString . T.fmap' (mkChunk TX.length . f . _fromChunk) . fromRope -- | Maps over each __chunk__ which means this function is UNSAFE! If -- you use this with functions which don't preserve 'Size', that is -- the chunk length and number of newlines, things will break really, -- really badly. You should not need to use this. -- -- Also see 'T.unsafeFmap' unsafeWithText :: (TX.Text -> TX.Text) -> YiString -> YiString unsafeWithText f = YiString . T.unsafeFmap g . fromRope where g (Chunk l t) = Chunk l (f t)