-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
…/feat/deprecate-v0
- Loading branch information
Showing
13 changed files
with
339 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@fuel-ts/abi-coder": patch | ||
"@fuel-ts/account": patch | ||
"@fuel-ts/utils": patch | ||
--- | ||
|
||
chore: remove `ethers` from `abi-coder` |
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
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
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,44 @@ | ||
import { FuelError, ErrorCode } from '@fuel-ts/errors'; | ||
|
||
import { toUtf8Bytes } from './toUtf8Bytes'; | ||
|
||
/** | ||
* @group node | ||
* @group browser | ||
*/ | ||
describe('toUtf8Bytes', () => { | ||
it('should convert a simple ASCII string to UTF-8 bytes', () => { | ||
const input = 'Hello, world!'; | ||
const expectedOutput = new Uint8Array([ | ||
72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, | ||
]); | ||
expect(toUtf8Bytes(input)).toEqual(expectedOutput); | ||
}); | ||
|
||
it('should convert a string with non-ASCII characters to UTF-8 bytes', () => { | ||
const input = 'Héllö, wórld!'; | ||
const expectedOutput = new Uint8Array([ | ||
72, 195, 169, 108, 108, 195, 182, 44, 32, 119, 195, 179, 114, 108, 100, 33, | ||
]); | ||
expect(toUtf8Bytes(input)).toEqual(expectedOutput); | ||
}); | ||
|
||
it('should handle surrogate pairs correctly', () => { | ||
const input = '😀'; | ||
const expectedOutput = new Uint8Array([240, 159, 152, 128]); | ||
expect(toUtf8Bytes(input)).toEqual(expectedOutput); | ||
}); | ||
|
||
it('should throw an error for invalid surrogate pairs', () => { | ||
const input = '\uD800'; | ||
expect(() => toUtf8Bytes(input)).toThrowError( | ||
new FuelError(ErrorCode.INVALID_INPUT_PARAMETERS, 'Invalid UTF-8 in the input string.') | ||
); | ||
}); | ||
|
||
it('should normalize the input string', () => { | ||
const input = 'e\u0301'; | ||
const expectedOutput = new Uint8Array([195, 169]); | ||
expect(toUtf8Bytes(input)).toEqual(expectedOutput); | ||
}); | ||
}); |
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,53 @@ | ||
import { FuelError, ErrorCode } from '@fuel-ts/errors'; | ||
|
||
/** | ||
* Returns the UTF-8 byte representation of str. | ||
* | ||
* If form is disabled, the string is not normalized. | ||
* @param stri - the string to convert to UTF-8 bytes. | ||
* @param form - whether to normalize the string. | ||
* @returns - the UTF-8 byte representation of str. | ||
*/ | ||
export function toUtf8Bytes(stri: string, form = true): Uint8Array { | ||
let str = stri; | ||
|
||
if (form) { | ||
str = stri.normalize('NFC'); | ||
} | ||
|
||
const result: Array<number> = []; | ||
|
||
for (let i = 0; i < str.length; i += 1) { | ||
const c = str.charCodeAt(i); | ||
|
||
if (c < 0x80) { | ||
result.push(c); | ||
} else if (c < 0x800) { | ||
result.push((c >> 6) | 0xc0); | ||
result.push((c & 0x3f) | 0x80); | ||
} else if ((c & 0xfc00) === 0xd800) { | ||
i += 1; | ||
const c2 = str.charCodeAt(i); | ||
|
||
if (i >= str.length || (c2 & 0xfc00) !== 0xdc00) { | ||
throw new FuelError( | ||
ErrorCode.INVALID_INPUT_PARAMETERS, | ||
'Invalid UTF-8 in the input string.' | ||
); | ||
} | ||
|
||
// Surrogate Pair | ||
const pair = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff); | ||
result.push((pair >> 18) | 0xf0); | ||
result.push(((pair >> 12) & 0x3f) | 0x80); | ||
result.push(((pair >> 6) & 0x3f) | 0x80); | ||
result.push((pair & 0x3f) | 0x80); | ||
} else { | ||
result.push((c >> 12) | 0xe0); | ||
result.push(((c >> 6) & 0x3f) | 0x80); | ||
result.push((c & 0x3f) | 0x80); | ||
} | ||
} | ||
|
||
return new Uint8Array(result); | ||
} |
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,47 @@ | ||
import { toUtf8String } from './toUtf8String'; | ||
|
||
/** | ||
* @group node | ||
* @group browser | ||
*/ | ||
describe('toUtf8String', () => { | ||
it('should convert valid UTF-8 bytes to a string', () => { | ||
const bytes = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]); | ||
expect(toUtf8String(bytes)).toEqual('Hello World'); | ||
}); | ||
|
||
it('should handle multi-byte characters', () => { | ||
const bytes = new Uint8Array([0xe2, 0x82, 0xac]); // '€' symbol | ||
expect(toUtf8String(bytes)).toEqual('€'); | ||
}); | ||
|
||
it('should handle invalid continuation bytes', () => { | ||
const bytes = new Uint8Array([0xc2, 0x61]); // Invalid continuation byte | ||
expect(toUtf8String(bytes)).toEqual(''); | ||
}); | ||
|
||
it('should handle overlong sequences', () => { | ||
const bytes = new Uint8Array([0xc0, 0xaf]); // Overlong sequence | ||
expect(toUtf8String(bytes)).toEqual(''); | ||
}); | ||
|
||
it('should handle out-of-range code points', () => { | ||
const bytes = new Uint8Array([0xf4, 0x90, 0x80, 0x80]); // Out-of-range code point | ||
expect(toUtf8String(bytes)).toEqual(''); | ||
}); | ||
|
||
it('should handle UTF-16 surrogate code points', () => { | ||
const bytes = new Uint8Array([0xed, 0xa0, 0x80]); // UTF-16 surrogate code point | ||
expect(toUtf8String(bytes)).toEqual(''); | ||
}); | ||
|
||
it('should handle missing continuation bytes', () => { | ||
const bytes = new Uint8Array([0xe2, 0x82]); // Missing continuation byte | ||
expect(toUtf8String(bytes)).toEqual(''); | ||
}); | ||
|
||
it('should handle overrun bytes', () => { | ||
const bytes = new Uint8Array([0xe2]); // Overrun byte | ||
expect(toUtf8String(bytes)).toEqual(''); | ||
}); | ||
}); |
Oops, something went wrong.