File tree Expand file tree Collapse file tree 3 files changed +81
-0
lines changed Expand file tree Collapse file tree 3 files changed +81
-0
lines changed Original file line number Diff line number Diff line change 1+ //===----------------------------------------------------------------------===//
2+ //
3+ // This source file is part of the Swift.org open source project
4+ //
5+ // Copyright (c) 2014 - 2024 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 abstraction for sanitized values on a token.
14+ public struct Identifier {
15+ /// The sanitized `text` of a token.
16+ public let name : String
17+
18+ public init ( _ token: TokenSyntax ) {
19+ let text = token. text
20+
21+ self . name =
22+ if text. contains ( " ` " ) {
23+ String ( text. trimmingCharacters ( in: " ` " ) )
24+ } else {
25+ text
26+ }
27+ }
28+ }
Original file line number Diff line number Diff line change @@ -120,3 +120,29 @@ extension RawUnexpectedNodesSyntax {
120120 self . init ( raw: raw)
121121 }
122122}
123+
124+ extension String {
125+ func trimmingCharacters( in charactersToTrim: any BidirectionalCollection < Character > ) -> Substring {
126+ let leadingCharacters = countOfSequentialCharacters ( charactersToTrim, in: self )
127+ let trailingCharacters = countOfSequentialCharacters ( charactersToTrim, in: reversed ( ) )
128+
129+ return dropFirst ( leadingCharacters) . dropLast ( trailingCharacters)
130+ }
131+ }
132+
133+ private func countOfSequentialCharacters(
134+ _ charactersToCount: any BidirectionalCollection < Character > ,
135+ in characters: any BidirectionalCollection < Character >
136+ ) -> Int {
137+ var count = 0
138+
139+ for character in characters {
140+ if charactersToCount. contains ( character) {
141+ count += 1
142+ } else {
143+ break
144+ }
145+ }
146+
147+ return count
148+ }
Original file line number Diff line number Diff line change 1+ //===----------------------------------------------------------------------===//
2+ //
3+ // This source file is part of the Swift.org open source project
4+ //
5+ // Copyright (c) 2014 - 2024 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+ import SwiftSyntax
14+ import XCTest
15+
16+ class IdentifierTests : XCTestCase {
17+ public func testInit( ) {
18+ let basicToken = TokenSyntax ( stringLiteral: " sometoken " )
19+ XCTAssertEqual ( Identifier ( basicToken) . name, " sometoken " )
20+
21+ let backtickedToken = TokenSyntax ( stringLiteral: " `backtickedtoken` " )
22+ XCTAssertEqual ( Identifier ( backtickedToken) . name, " backtickedtoken " )
23+
24+ let multiBacktickedToken = TokenSyntax ( stringLiteral: " ```backtickedtoken``` " )
25+ XCTAssertEqual ( Identifier ( multiBacktickedToken) . name, " backtickedtoken " )
26+ }
27+ }
You can’t perform that action at this time.
0 commit comments