Created
October 12, 2017 14:54
-
-
Save davidair/1d2ee80d96ace6562058271bc90c155a to your computer and use it in GitHub Desktop.
Example of an easier string in Swift (not optimized)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2017 Google Inc. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
https://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
class EasyStringIterator : IteratorProtocol { | |
var _index:Int = -1 | |
var _characters : [Character] | |
init(_ characters: [Character]) { | |
_characters = characters | |
} | |
func next() -> Character? { | |
if (_index >= _characters.count - 1) { | |
return nil | |
} | |
else { | |
_index = _index + 1 | |
return _characters[_index] | |
} | |
} | |
} | |
class EasyString :CustomStringConvertible, Sequence { | |
var _characters : [Character] | |
init(_ str:String) { | |
_characters = Array(str) | |
} | |
public var description: String { | |
return String(_characters) | |
} | |
subscript(index:Int) -> Character { | |
get { | |
return _characters[index] | |
} | |
set(newElm) { | |
_characters[index] = newElm | |
} | |
} | |
func makeIterator() -> EasyStringIterator { | |
return EasyStringIterator(_characters) | |
} | |
static func +(left: EasyString, right: EasyString) -> EasyString { | |
return EasyString(left.description + right.description) | |
} | |
static func +(left: EasyString, right: String) -> EasyString { | |
return EasyString(left.description + right) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment