-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add buffer.transcode(source, from, to) method. Primarily uses ICU to transcode a buffer's content from one of Node.js' supported encodings to another. Originally part of a proposal to add a new unicode module. Decided to refactor the approach towrds individual PRs without a new module. Refs: #8075 PR-URL: #9038 Reviewed-By: Anna Henningsen <anna@addaleax.net>
- Loading branch information
Showing
9 changed files
with
437 additions
and
39 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
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,30 @@ | ||
'use strict'; | ||
|
||
if (!process.binding('config').hasIntl) { | ||
return; | ||
} | ||
|
||
const normalizeEncoding = require('internal/util').normalizeEncoding; | ||
const Buffer = require('buffer').Buffer; | ||
|
||
const icu = process.binding('icu'); | ||
|
||
// Transcodes the Buffer from one encoding to another, returning a new | ||
// Buffer instance. | ||
exports.transcode = function transcode(source, fromEncoding, toEncoding) { | ||
if (!Buffer.isBuffer(source)) | ||
throw new TypeError('"source" argument must be a Buffer'); | ||
if (source.length === 0) return Buffer.alloc(0); | ||
|
||
fromEncoding = normalizeEncoding(fromEncoding) || fromEncoding; | ||
toEncoding = normalizeEncoding(toEncoding) || toEncoding; | ||
const result = icu.transcode(source, fromEncoding, toEncoding); | ||
if (Buffer.isBuffer(result)) | ||
return result; | ||
|
||
const code = icu.icuErrName(result); | ||
const err = new Error(`Unable to transcode Buffer [${code}]`); | ||
err.code = code; | ||
err.errno = result; | ||
throw err; | ||
}; |
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
Oops, something went wrong.