-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle invalid utf8 sequence from Whisper for Dart API. (#1106)
Fixes #1104
- Loading branch information
1 parent
08c7585
commit 5a2603f
Showing
4 changed files
with
33 additions
and
2 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
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,25 @@ | ||
// Copyright (c) 2024 Xiaomi Corporation | ||
import 'dart:convert'; | ||
import 'dart:ffi'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:ffi/ffi.dart'; | ||
|
||
int _strLen(Pointer<Uint8> codeUnits) { | ||
// this function is copied from | ||
// https://github.com/dart-archive/ffi/blob/main/lib/src/utf8.dart#L52 | ||
var length = 0; | ||
while (codeUnits[length] != 0) { | ||
length++; | ||
} | ||
return length; | ||
} | ||
|
||
// This function is modified from | ||
// https://github.com/dart-archive/ffi/blob/main/lib/src/utf8.dart#L41 | ||
// It ignores invalid utf8 sequence | ||
String toDartString(Pointer<Utf8> s) { | ||
final codeUnits = s.cast<Uint8>(); | ||
final length = _strLen(codeUnits); | ||
return utf8.decode(codeUnits.asTypedList(length), allowMalformed: true); | ||
} |