An open API service indexing awesome lists of open source software.

https://github.com/perfectsiderepos/perfect-iconv

A Swif Class Wrapper for ICONV, Inspired by Yasuhiro Hatta
https://github.com/perfectsiderepos/perfect-iconv

Last synced: 8 months ago
JSON representation

A Swif Class Wrapper for ICONV, Inspired by Yasuhiro Hatta

Awesome Lists containing this project

README

          

# Perfect ICONV [简体中文](README.zh_CN.md)



Get Involed with Perfect!



Star Perfect On Github


Stack Overflow


Follow Perfect on Twitter


Join the Perfect Slack



Swift 4.1


Platforms OS X | Linux


License Apache


PerfectlySoft Twitter


Slack Status

Swift Class Wrapper for ICONV, inspired by Yasuhiro Hatta's Iconv Project. See [https://github.com/yaslab/Iconv](https://github.com/yaslab/Iconv) for details.

This package builds with Swift Package Manager and is part of the [Perfect](https://github.com/PerfectlySoft/Perfect) project.

## Demo

``` swift
import PerfectICONV

do {
let i = try Iconv()
let bytes:[UInt8] = [0xd6, 0xd0, 0xb9, 0xfa, 0x0a]
guard let cn = i.utf8(buf: bytes) else {
XCTFail("fault")
return
}//end guard
print(cn)
XCTAssertTrue(cn.hasPrefix("中国"))
}catch(let err) {
XCTFail("ERROR: \(err)")
}
```

## Quick Start

### Swift Package Manager

Add a dependency to Package.swift:

``` swift
.Package(url: "https://github.com/PerfectSideRepos/Perfect-ICONV.git",
majorVersion:3)
```

### Header Declaration

Import iconv lib to your source code:

``` swift
import PerfectICONV
```

### Initialization

Set the code pages before transforming encoding from one to another:

``` swift
do {
let iconv = try Iconv(from: .GB2312, to: .UTF_8)
}catch(let err) {
/// something goes wrong here, e.g., invalid code page, etc.
}
```
*NOTE*: Code Page constants could be found on source code of this project with keyword of `enum`:
``` swift
public enum CodePage: String {
case US = "US"
case US_ASCII = "US-ASCII"
case CSASCII = "CSASCII"
case UTF_8 = "UTF-8"
case UTF8 = "UTF8"
...
}
```
### Conversions

PerfectICONV has a few express ways of encoding conversions:

- `iconv.utf8(bytes: [Int8])` or `iconv.utf8(bytes: [UInt8])`: directly convert a signed or unsigned byte buffer from the source code page to utf-8

``` swift
let bytes:[UInt8] = [0xd6, 0xd0, 0xb9, 0xfa, 0x0a]
guard let china = iconv.utf8(buf: bytes) else {
/// something wrong
}//end guard
// if ok, it will print "中国"
print(china)
```

- `iconv.convert(buf: [Int8]) -> [Int8]` or `iconv.convert(buf: [UInt8]) -> [UInt8]`: convert codepages from one byte buffer to another

``` swift
let bytes:[UInt8] = [0xd6, 0xd0, 0xb9, 0xfa, 0x0a]
let chinaBytes = iconv.convert(buf: bytes)
// if nothing wrong, the chinaBytes is now an array of UInt8 which contains the expected encoding.
```

- `iconv.convert(buf: UnsafePointer, length: Int) -> (UnsafeMutablePointer?, Int)`: similar to Mr. Hatta's api design, convert a source encoding from a pointer with length to the objective tuple.
⚠️*NOTE*⚠️ YOU MUST MANUALLY DEALLOCATE THE OUTCOME POINTER.

## Further Information
For more information on the Perfect project, please visit [perfect.org](http://perfect.org).