daily pastebin goal
86%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  Swift is a lovely-looking language, but I really don't like how it doesn't
  3.  let you slice strings by grapheme and makes you create and pass in
  4.  un-ergonomic index types instead...
  5.  I recognize that unicode has a bunch of different sizes of strings, which
  6.  is why Swift doesn't wanna do that.  But for small strings, which are the
  7.  kind where one might reasonably want to do a lot of Python-style slicing,
  8.  the perf cost of just iterating to get slices all the time seems, to me,
  9.  to be at least sometimes outweighed by the ergonomics on the code-writing
  10.  side.
  11.  So here's some slicing, by force. I'm just monkey-patching String here,
  12.  basically.
  13.  */
  14.  
  15. var s = "here is a boring string"
  16.  
  17. extension String {
  18.     func getCharList() -> [Character] {
  19.         var characters = [Character]()
  20.         for character in self {
  21.             characters.append(character)
  22.         }
  23.         return characters
  24.     }
  25.     subscript(singleSlice: Int) -> String {
  26.         switch singleSlice{
  27.         case ..<0:
  28.             return String(self.getCharList().reversed()[singleSlice + 1])
  29.         default:
  30.             return String(self.getCharList()[singleSlice])
  31.         }
  32.     }
  33.     subscript(start: Int, stop: Int) -> String {
  34.         switch (start, stop){
  35.         case let (x, y) where x > y:
  36.             return String(self.getCharList()[stop..<start].reversed())
  37.         default:
  38.             return String(self.getCharList()[start..<stop])
  39.         }
  40.     }
  41.     subscript(range: CountableRange<Int>) -> String {
  42.         return String(self.getCharList()[range])
  43.     }
  44.     subscript(range: CountableClosedRange<Int>) -> String {
  45.         return String(self.getCharList()[range])
  46.     }
  47.     subscript(range: PartialRangeThrough<Int>) -> String {
  48.         return String(self.getCharList()[range])
  49.     }
  50.     subscript(range: CountablePartialRangeFrom<Int>) -> String {
  51.         return String(self.getCharList()[range])
  52.     }
  53.     subscript(range: PartialRangeUpTo<Int>) -> String {
  54.         return String(self.getCharList()[range])
  55.     }
  56. }
  57.  
  58. /* there's probably some higher level generic protocol or something
  59.  I can swap in for all these ranges, but I don't know it. Still learning. */
  60.  
  61. print(s.getCharList())
  62. print(s[1])
  63. print(s[-1])
  64. print(s[0, 5])
  65. print(s[5, 0])
  66. print(s[3...6])
  67. print(s[2..<10])
  68. print(s[...15])
  69. print(s[2...])
  70. print(s[..<15])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top