-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from mtgto/euc-jis-2004-data-extension
EUC-JISX0213の文字列の読み込みをDataから行うようにする
- Loading branch information
Showing
5 changed files
with
76 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-FileCopyrightText: 2024 mtgto <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import Foundation | ||
|
||
enum EucJis2004Error: Error { | ||
case unsupported | ||
case convert | ||
} | ||
|
||
extension Data { | ||
/** | ||
* libiconvを使ってEUC-JPの拡張であるEUC-JISX0213としてデコードする。 | ||
*/ | ||
func eucJis2004String() throws -> String { | ||
if isEmpty { | ||
return "" | ||
} | ||
let cd = iconv_open("UTF-8".cString(using: .ascii), "EUC-JISX0213".cString(using: .ascii)) | ||
if cd == iconv_t(bitPattern: -1) { | ||
logger.error("iconvの初期化に失敗しました") | ||
throw EucJis2004Error.unsupported | ||
} | ||
defer { | ||
if iconv_close(cd) == -1 { | ||
logger.error("iconv変換ディスクリプタの解放に失敗しました: \(errno)") | ||
} | ||
} | ||
var data = self | ||
var inLeft = data.count | ||
// EUC-JIS-2004は1文字で1..2バイト (ASCIIは1バイト)、UTF-8は1..4バイト (ASCIIは1バイト) なのでバッファサイズは2倍用意する | ||
var outLeft = data.count * 2 | ||
var buffer = Array<CChar>(repeating: 0, count: outLeft) | ||
return try data.withUnsafeMutableBytes { | ||
var inPtr = $0.baseAddress?.assumingMemoryBound(to: CChar.self) | ||
try buffer.withUnsafeMutableBufferPointer { | ||
var outPtr = $0.baseAddress | ||
let ret = iconv(cd, &inPtr, &inLeft, &outPtr, &outLeft) | ||
if ret == -1 { | ||
if errno == EBADF { | ||
logger.error("iconv変換ディスクリプタの状態が異常です") | ||
} else if errno == EILSEQ { | ||
logger.error("入力に不正なバイト列が存在します") | ||
} else if errno == E2BIG { | ||
logger.error("EUC-JIS-2004からの変換先のバッファが足りません") | ||
} else if errno == EINVAL { | ||
logger.error("入力文字列が終端していません") | ||
} | ||
throw EucJis2004Error.convert | ||
} else if ret > 0 { | ||
logger.warning("EUC-JIS-2004から処理できない文字が \(ret) 文字ありました") | ||
} | ||
} | ||
guard let str = String(validatingUTF8: buffer) else { | ||
throw EucJis2004Error.convert | ||
} | ||
return str | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters