|
| 1 | +//===--------------- AbsolutePosition.swift - Source Positions ------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +/// An absolute position in a source file as text - the absolute utf8Offset from |
| 14 | +/// the start, line, and column. |
| 15 | +public final class AbsolutePosition { |
| 16 | + public fileprivate(set) var utf8Offset: Int |
| 17 | + public fileprivate(set) var line: Int |
| 18 | + public fileprivate(set) var column: Int |
| 19 | + |
| 20 | + public init(line: Int = 1, column: Int = 1, utf8Offset: Int = 0) { |
| 21 | + self.line = line |
| 22 | + self.column = column |
| 23 | + self.utf8Offset = utf8Offset |
| 24 | + } |
| 25 | + |
| 26 | + internal func add(columns: Int) { |
| 27 | + print("adding \(columns) column bytes") |
| 28 | + self.column += columns |
| 29 | + } |
| 30 | + |
| 31 | + internal func add(lines: Int, size: Int) { |
| 32 | + print("adding \(lines * size) line bytes") |
| 33 | + self.line += lines * size |
| 34 | + self.column = 1 |
| 35 | + } |
| 36 | + |
| 37 | + /// Use some text as a reference for adding to the absolute position, |
| 38 | + /// taking note of newlines, etc. |
| 39 | + internal func add(text: String) { |
| 40 | + print("adding text: \(text)") |
| 41 | + for char in text { |
| 42 | + switch char { |
| 43 | + case "\n", "\r\n": |
| 44 | + line += 1 |
| 45 | + column = 1 |
| 46 | + default: |
| 47 | + column += 1 |
| 48 | + } |
| 49 | + |
| 50 | + // FIXME: This is currently very wasteful, but should be fast once the |
| 51 | + // small-string optimization lands. |
| 52 | + utf8Offset += String(char).utf8.count |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + internal func copy() -> AbsolutePosition { |
| 57 | + return AbsolutePosition(line: line, column: column, |
| 58 | + utf8Offset: utf8Offset) |
| 59 | + } |
| 60 | +} |
0 commit comments