StringLocationConverter

master

Converts String index values into their corresponding line & column numbers
SwiftDocOrg/StringLocationConverter

StringLocationConverter

A Swift package for converting String.Index values into their corresponding line and column numbers.

Usage

import Foundation
import StringLocationConverter

let string = """
# [Universal Declaration of Human Rights][udhr]

## Article 1.

All human beings are born free and equal in dignity and rights.
They are endowed with reason and conscience
and should act towards one another in a spirit of brotherhood.

[udhr]: https://www.un.org/en/universal-declaration-human-rights/ "View full version"
"""

let range = string.range(of: "reason")!

let converter = StringLocationConverter(for: string)
let position = converter.location(for: range.lowerBound, in: string)!
position.line // => Int = 6
position.column // => Int = 23

Installation

Swift Package Manager

Add the StringLocationConverter package to your target dependencies in Package.swift:

import PackageDescription

let package = Package(
  name: "YourProject",
  dependencies: [
    .package(
        url: "https://github.com/SwiftDocOrg/StringLocationConverter",
        from: "0.0.1"
    ),
  ]
)

Add StringLocationConverter as a dependency to your target(s):

targets: [
.target(
    name: "YourTarget",
    dependencies: ["StringLocationConverter"]),

License

MIT

Contact

Mattt (@mattt)

Description

  • Swift Tools 5.2.0
View More Packages from this Author

Dependencies

  • None
Last updated: Wed Jan 10 2024 11:50:07 GMT-1000 (Hawaii-Aleutian Standard Time)