Skip to content

Commit

Permalink
辞書からの要素削除で複数の変換候補をもつ読みから削除すると読みリストから消してしまうバグの修正
Browse files Browse the repository at this point in the history
  • Loading branch information
mtgto committed Oct 23, 2024
1 parent 39327c4 commit 274ec64
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
24 changes: 13 additions & 11 deletions macSKK/MemoryDict.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,22 @@ struct MemoryDict: DictProtocol {
/// - Returns: エントリを削除できたかどうか
mutating func delete(yomi: String, word: Word.Word) -> Bool {
if let words = entries[yomi] {
if words.count == 1 {
if yomi.isOkuriAri {
if let index = okuriAriYomis.firstIndex(of: yomi) {
okuriAriYomis.remove(at: index)
let filtered = words.filter { $0.word != word }
if words.count != filtered.count {
if filtered.isEmpty {
entries.removeValue(forKey: yomi)
if yomi.isOkuriAri {
if let index = okuriAriYomis.firstIndex(of: yomi) {
okuriAriYomis.remove(at: index)
}
} else {
if let index = okuriNashiYomis.firstIndex(of: yomi) {
okuriNashiYomis.remove(at: index)
}
}
} else {
if let index = okuriNashiYomis.firstIndex(of: yomi) {
okuriNashiYomis.remove(at: index)
}
entries[yomi] = filtered
}
}
let filtered = words.filter { $0.word != word }
if words.count != filtered.count {
entries[yomi] = filtered
return true
}
}
Expand Down
5 changes: 5 additions & 0 deletions macSKKTests/MemoryDictTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class MemoryDictTests: XCTestCase {

func testDelete() throws {
var dict = MemoryDict(entries: ["あr": [Word(""), Word("")], "": [Word(""), Word("")]], readonly: false)
XCTAssertFalse(dict.entries.isEmpty)
XCTAssertEqual(dict.okuriAriYomis, ["あr"])
XCTAssertEqual(dict.okuriNashiYomis, [""])
XCTAssertFalse(dict.delete(yomi: "あr", word: ""))
Expand All @@ -124,10 +125,14 @@ class MemoryDictTests: XCTestCase {
XCTAssertEqual(dict.refer("あr", option: nil), [Word("")])
XCTAssertFalse(dict.delete(yomi: "いいい", word: "いいい"))
XCTAssertFalse(dict.delete(yomi: "あr", word: ""), "削除済")
XCTAssertEqual(dict.okuriAriYomis, ["あr"])
XCTAssertTrue(dict.delete(yomi: "あr", word: ""))
XCTAssertEqual(dict.okuriAriYomis, [])
XCTAssertFalse(dict.delete(yomi: "", word: ""), "削除済")
XCTAssertEqual(dict.okuriNashiYomis, [""])
XCTAssertTrue(dict.delete(yomi: "", word: ""))
XCTAssertEqual(dict.okuriNashiYomis, [])
XCTAssertTrue(dict.entries.isEmpty)
}

func testDeleteOkuriBlock() throws {
Expand Down

0 comments on commit 274ec64

Please sign in to comment.