Skip to content

Commit

Permalink
Improve regex
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed May 29, 2023
1 parent 336b663 commit e7df8fa
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 14 deletions.
61 changes: 47 additions & 14 deletions SWDestinyTrades/Classes/CardDetail/View/DieFaceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ final class DieFaceView: UIView {
fatalError("init(coder:) has not been implemented")
}

// swiftlint:disable:next cyclomatic_complexity
internal func configureDie(face: String) {
if face.contains("+") {
symbol.tintColor = .blue
Expand All @@ -70,36 +69,70 @@ final class DieFaceView: UIView {
allowed.formUnion(CharacterSet.decimalDigits)
allowed.formUnion(CharacterSet.symbols)
allowed.formUnion(CharacterSet(charactersIn: "X"))
quantity.text = face.trimmingCharacters(in: allowed.inverted)

print("[Debug] Input: \(face)")
if let subtitle = getSubtitle(input: face) {
print("[Debug] Subtitle: \(subtitle)")
symbol.image = subtitle
}
}

let face2 = face.trimmingCharacters(in: allowed)
func getSubtitle(input: String) -> UIImage? {
let pattern = "(?i)(\\d+)?([A-Z\\-]+)(\\d+)?(\\b|(?=[A-Z\\-])|$)"

do {
let regex = try NSRegularExpression(pattern: pattern)
let matches = regex.matches(in: input, range: NSRange(input.startIndex..., in: input))

if let match = matches.first {
for index in 2 ..< match.numberOfRanges {
let nsRange = match.range(at: index)
if let range = Range(nsRange, in: input) {
let option = String(input[range])
let image = assignImage(face: option)
if image != nil {
return image
}
}
}
}
} catch {
print("Error: Invalid regex pattern")
}

return nil
}

switch face2.uppercased() {
func assignImage(face: String) -> UIImage? {
print("[Debug] face:" + face)
switch face.uppercased() {
case "MD":
symbol.image = Asset.icMelee.image.withRenderingMode(.alwaysTemplate)
return Asset.icMelee.image.withRenderingMode(.alwaysTemplate)
case "RD":
symbol.image = Asset.icRanged.image.withRenderingMode(.alwaysTemplate)
return Asset.icRanged.image.withRenderingMode(.alwaysTemplate)
case "R":
symbol.image = Asset.icResource.image.withRenderingMode(.alwaysTemplate)
return Asset.icResource.image.withRenderingMode(.alwaysTemplate)
case "DR":
symbol.image = Asset.icDisrupt.image.withRenderingMode(.alwaysTemplate)
return Asset.icDisrupt.image.withRenderingMode(.alwaysTemplate)
case "-":
symbol.image = Asset.icBlank.image.withRenderingMode(.alwaysTemplate)
quantity.isHidden = true
return Asset.icBlank.image.withRenderingMode(.alwaysTemplate)
case "SP":
symbol.image = Asset.icSpecial.image.withRenderingMode(.alwaysTemplate)
quantity.isHidden = true
return Asset.icSpecial.image.withRenderingMode(.alwaysTemplate)
case "F":
symbol.image = Asset.icFocus.image.withRenderingMode(.alwaysTemplate)
return Asset.icFocus.image.withRenderingMode(.alwaysTemplate)
case "SH":
symbol.image = Asset.icShield.image.withRenderingMode(.alwaysTemplate)
return Asset.icShield.image.withRenderingMode(.alwaysTemplate)
case "DC":
symbol.image = Asset.icDiscard.image.withRenderingMode(.alwaysTemplate)
return Asset.icDiscard.image.withRenderingMode(.alwaysTemplate)
case "ID":
symbol.image = Asset.icIndirect.image.withRenderingMode(.alwaysTemplate)
return Asset.icIndirect.image.withRenderingMode(.alwaysTemplate)
default:
symbol.isHidden = true
return nil
}
quantity.text = face.trimmingCharacters(in: allowed.inverted)
}
}

Expand Down
80 changes: 80 additions & 0 deletions SWDestinyTrades/Classes/CardDetail/View/DieSymbolView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// DieSymbolView.swift
// SWDestinyTrades
//
// Created by Diogo Autilio on 08/09/22.
// Copyright © 2022 Diogo Autilio. All rights reserved.
//

import Foundation
import UIKit

final class DieSymbolView: UIStackView {

var quantity: UILabel = {
let label = UILabel(frame: .zero)
label.font = .boldSystemFont(ofSize: 20)
label.textAlignment = .center
label.textColor = .black
label.setContentHuggingPriority(.defaultHigh, for: .horizontal)
label.adjustsFontSizeToFitWidth = true
return label
}()

var symbol: UIImageView = {
let imageView = UIImageView(frame: .zero)
imageView.contentMode = .scaleAspectFit
return imageView
}()

init(face: String) {
super.init(frame: .zero)
axis = .horizontal
distribution = .fillEqually
alignment = .center
buildViewHierarchy()
setupConstraints()
configureViews()
configureDie(face: face)
}

@available(*, unavailable)
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

internal func configureDie(face: String) {}
}

extension DieSymbolView: BaseViewConfiguration {

internal func buildViewHierarchy() {
addArrangedSubview(quantity)
addArrangedSubview(symbol)
}

internal func setupConstraints() {
// faceContainer.layout.applyConstraint { make in
// make.topAnchor(equalTo: topAnchor)
// make.leadingAnchor(equalTo: leadingAnchor)
// make.bottomAnchor(equalTo: bottomAnchor)
// make.trailingAnchor(equalTo: trailingAnchor)
// }
//
// stackView.layout.applyConstraint { make in
// make.topAnchor(equalTo: faceContainer.topAnchor)
// make.leadingAnchor(equalTo: faceContainer.leadingAnchor, constant: 8)
// make.bottomAnchor(equalTo: faceContainer.bottomAnchor)
// make.trailingAnchor(equalTo: faceContainer.trailingAnchor, constant: -8)
// }

symbol.layout.applyConstraint { make in
make.heightAnchor(equalToConstant: 25)
make.widthAnchor(equalToConstant: 25)
}
}

internal func configureViews() {
backgroundColor = .clear
}
}
43 changes: 43 additions & 0 deletions SWDestinyTrades/Classes/Utils/Regex.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Regex.swift
// SWDestinyTrades
//
// Created by Diogo Autilio on 19/08/22.
// Copyright © 2022 Diogo Autilio. All rights reserved.
//

import Foundation

struct Regex {

let pattern: String
let options: NSRegularExpression.Options

private var matcher: NSRegularExpression {
return try! NSRegularExpression(pattern: pattern, options: options) // swiftlint:disable:this force_try
}

init(pattern: String, options: NSRegularExpression.Options = []) {
self.pattern = pattern
self.options = options
}

func match(_ string: String, options: NSRegularExpression.MatchingOptions = []) -> Bool {
return matcher.numberOfMatches(in: string, options: options, range: NSRange(location: 0, length: string.utf16.count)) != 0
}
}

protocol RegularExpressionMatchable {
func match(_ regex: Regex) -> Bool
}

extension String: RegularExpressionMatchable {

func match(_ regex: Regex) -> Bool {
return regex.match(self)
}
}

func ~= <T: RegularExpressionMatchable>(pattern: Regex, matchable: T) -> Bool {
return matchable.match(pattern)
}

0 comments on commit e7df8fa

Please sign in to comment.