Skip to content

Commit

Permalink
Fix String+Latin underline issue
Browse files Browse the repository at this point in the history
Add String Distance
  • Loading branch information
MohammadRezaAnsari committed Dec 20, 2021
1 parent 9751d40 commit 0fa44b2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// AttributedString+Attribute.swift
//
//
// Created by MohammadReza Ansary on 12/20/21.
//

import Foundation

public extension NSMutableAttributedString {
func attribute(_ key: NSAttributedString.Key, value: Any, range: NSRange) -> Self {
addAttribute(key, value: value, range: range)
return self
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// String+Attributed.swift
//
//
// Created by MohammadReza Ansary on 12/20/21.
//

import Foundation

public extension String {
var attributed: NSMutableAttributedString {
NSMutableAttributedString(string: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// String+Distance.swift
//
//
// Created by MohammadReza Ansary on 12/20/21.
//

import Foundation

public extension StringProtocol {
func distance(of element: Element) -> Int? { firstIndex(of: element)?.distance(in: self) }
func distance<S: StringProtocol>(of string: S) -> Int? { range(of: string)?.lowerBound.distance(in: self) }
}


public extension Collection {
func distance(to index: Index) -> Int { distance(from: startIndex, to: index) }
}


public extension String.Index {
func distance<S: StringProtocol>(in string: S) -> Int { string.distance(to: self) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public extension String {

/// Perform string transliteration to latin.
var latin: Self {
self.applyingTransform(StringTransform.toLatin, reverse: false) ?? self
}
let latin = self.applyingTransform(StringTransform.toLatin, reverse: false) ?? self
return latin.applyingTransform(StringTransform.stripDiacritics, reverse: false) ?? self
}
}

0 comments on commit 0fa44b2

Please sign in to comment.