Skip to content

Commit 1950943

Browse files
committed
Fixed a bug when pinyin formatter removed all characters that didn't match valid syllables
1 parent 607354b commit 1950943

7 files changed

Lines changed: 47 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## [2.9.9]
2+
- Fixed a bug when pinyin formatter removed all characters that didn't match valid syllables
13
## [2.9.7]
24
- Fixed a period at the end if mantissa length is 0 https://github.com/caseyryan/flutter_multi_formatter/issues/106
35
## [2.9.6]

example/lib/pages/pinyin_formatter_page.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_multi_formatter/flutter_multi_formatter.dart';
3-
import 'package:flutter_multi_formatter/utils/pinyin_utils.dart';
43

54
class PinyinFormatterPage extends StatelessWidget {
65
const PinyinFormatterPage({
@@ -9,8 +8,6 @@ class PinyinFormatterPage extends StatelessWidget {
98

109
@override
1110
Widget build(BuildContext context) {
12-
final result = PinyinUtils.splitToSyllablesBySeparator('xingh');
13-
print(result);
1411
return Unfocuser(
1512
child: Scaffold(
1613
appBar: AppBar(
@@ -27,6 +24,7 @@ class PinyinFormatterPage extends StatelessWidget {
2724
),
2825
SizedBox(height: 12.0),
2926
TextFormField(
27+
autocorrect: false,
3028
decoration: InputDecoration(
3129
border: const OutlineInputBorder(),
3230
hintText: 'Enter pinyin phrase',

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ packages:
9696
path: ".."
9797
relative: true
9898
source: path
99-
version: "2.9.7"
99+
version: "2.9.8"
100100
flutter_test:
101101
dependency: "direct dev"
102102
description: flutter

lib/formatters/phone_input_formatter.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,10 @@ class PhoneInputFormatter extends TextInputFormatter {
216216
existingList.add(m);
217217
});
218218
}
219-
print('Alternative masks for country "${countryData['country']}"' +
220-
' is now ${countryData['altMasks']}');
219+
// if (kDebugMode) {
220+
// print('Alternative masks for country "${countryData['country']}"' +
221+
// ' is now ${countryData['altMasks']}');
222+
// }
221223
}
222224

223225
/// Replaces an existing phone mask for the given country

lib/formatters/pinyin_formatter.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ class PinyinFormatter implements TextInputFormatter {
88
static final RegExp _apostropheRegexp = RegExp('\'');
99
static final RegExp _badApostrophes = RegExp(r"[’']+");
1010

11-
const PinyinFormatter();
11+
final String? replacementForSpace;
12+
13+
/// [replacementForSpace] in case you need to replace
14+
/// a space with something, just pass it here
15+
const PinyinFormatter({
16+
this.replacementForSpace,
17+
});
1218

1319
int _countSeparators(String value) {
1420
return _apostropheRegexp.allMatches(value).length;
@@ -22,13 +28,18 @@ class PinyinFormatter implements TextInputFormatter {
2228
final numOldSeparatos = _countSeparators(
2329
oldValue.text,
2430
);
31+
String initialText = newValue.text;
2532
String newText = newValue.text.replaceAll(_badApostrophes, '');
33+
if (replacementForSpace != null) {
34+
initialText = initialText.replaceAll(' ', replacementForSpace!);
35+
newText = newText.replaceAll(' ', replacementForSpace!);
36+
}
2637
final syllables = PinyinUtils.splitToSyllables<SyllableData>(
27-
newValue.text.trim(),
38+
newText.trim(),
2839
);
2940
newText = syllables.map((e) => e.value).join('\'');
3041
if (newText.isEmpty) {
31-
newText = newValue.text;
42+
newText = initialText;
3243
}
3344
if (newText.endsWith('\'')) {
3445
newText = newText.removeLast();

lib/utils/pinyin_utils.dart

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class SyllableData {
3434
}
3535

3636
class PinyinUtils {
37+
static const String UNICODE_SQUARE = '⬜';
38+
39+
static final RegExp _unstarredTextRegexp = RegExp(r'[^*]+');
3740
static final RegExp _punctuationRegex = RegExp(r"['!?.\[\],,。?!;:( )【 】[]]");
3841
static const _allSyllables = [
3942
"zhuang",
@@ -646,6 +649,7 @@ class PinyinUtils {
646649
value,
647650
allPossibleSentences: allPossibleSentences,
648651
);
652+
// print(allPossibleSentences);
649653
if (allPossibleSentences.isNotEmpty) {
650654
_Sentence? sentence;
651655
// print(allPossibleSentences);
@@ -902,10 +906,8 @@ class _Sentence {
902906
if (start < 0) {
903907
continue;
904908
}
905-
temp = temp.replaceFirst(
906-
syl,
907-
_getFiller(syl),
908-
);
909+
final filler = _getFiller(syl);
910+
temp = temp.replaceFirst(syl, filler);
909911
final realSyllable = initialValue.substring(
910912
start,
911913
end,
@@ -922,6 +924,24 @@ class _Sentence {
922924
),
923925
);
924926
}
927+
print(temp);
928+
final unstarred = PinyinUtils._unstarredTextRegexp;
929+
930+
/// заменяет оставшиеся символы, которые не совпали с валидными слогами
931+
final matches = unstarred.allMatches(temp);
932+
for (var m in matches) {
933+
final text = temp.substring(m.start, m.end);
934+
_correctSequence!.add(
935+
SyllableData(
936+
value: text,
937+
tone: -1,
938+
isValid: false,
939+
start: m.start,
940+
end: m.end,
941+
),
942+
);
943+
}
944+
925945
const invalidTone = -1;
926946
_correctSequence!.sort((a, b) => a.start.compareTo(b.start));
927947
final wrongSyllables = <SyllableData>[];

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_multi_formatter
22
description: A package of formatters for international phone numbers, credit / debit cards and a masked formatter
3-
version: 2.9.7
3+
version: 2.9.8
44
homepage: https://github.com/caseyryan/flutter_multi_formatter
55

66
environment:

0 commit comments

Comments
 (0)