kyle-n / HighlightedTextEditor

A SwiftUI view for dynamically highlighting user input
MIT License
726 stars 69 forks source link

Regular expression error with some characters from the text in a certain encoding (for example: utf-8) #50

Closed WheelaStudio closed 2 years ago

WheelaStudio commented 3 years ago

Describe the bug For example, if regular expression contains some symbols (e.x: И) from non-latinic alphabets(e.x: Russian) from text, which encoded in UTF-8, an error occurs in file HighlightingTextEditor.swift on 128 line Error text HighlightedTextEditor/HighlightingTextEditor.swift:128: Fatal error: Unexpectedly found nil while unwrapping an Optional value 2021-11-06 18:40:44.187051+0300 TextEditor[13196:2492169] HighlightedTextEditor/HighlightingTextEditor.swift:128: Fatal error: Unexpectedly found nil while unwrapping an Optional value

To Reproduce Steps to reproduce the behavior:

  1. Bind an string, which encoded in UTF-8
  2. Set in regular expression string, which contains "И"
  3. See error Screenshots image image

Environment Based on SwiftUI app, running on IOS 15.1

kyle-n commented 3 years ago

@WheelaStudio Thanks for reporting this. I'm still working on reproducing the bug.

This basic test project worked for me on Big Sur and iOS 15.0 in the simulator:

import SwiftUI
import HighlightedTextEditor

@main
struct TestingiOSApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

// matches text between underscores
let selection = try! NSRegularExpression(pattern: "ян", options: [])

struct ContentView: View {

    // snippets grabbed from google
    @State var text: String = "Привет Россиянин"

    var body: some View {
        VStack {
            HighlightedTextEditor(text: $text, highlightRules: [
                HighlightRule(pattern: selection, formattingRule: TextFormattingRule(key: .foregroundColor, value: UIColor.red))
            ])
        }
    }
}

Result:

simulator results

Could you modify the above code snippet so that it triggers the bug? I have a feeling I'm building the string wrong somehow.

WheelaStudio commented 3 years ago

I think it's problem in some files, which text was encoded incorrectly. I will check the program for another week.