Port Bug 1543077 part 2+3 - Follow changes to Japenese charset detection
Categories
(MailNews Core :: General, task)
Tracking
(Not tracked)
People
(Reporter: jorgk-bmo, Assigned: jorgk-bmo)
Details
Attachments
(2 files, 3 obsolete files)
|
5.20 KB,
patch
|
hsivonen
:
review+
|
Details | Diff | Splinter Review |
|
7.99 KB,
patch
|
jorgk-bmo
:
review+
|
Details | Diff | Splinter Review |
Quite a bit of bustage there.
| Assignee | ||
Comment 1•7 years ago
•
|
||
This compiles.
Henri, you took away the Japanese detection here:
https://hg.mozilla.org/mozilla-central/rev/f6d04ade73b5#l4.67
So how does Japanese detection work now? We have a test for it here which fail now:
https://searchfox.org/comm-central/rev/ba9e556dd988305051cd54ba1c6c13176e797f61/mailnews/compose/test/unit/test_detectAttachmentCharset.js#40
And maybe this will also fail:
https://searchfox.org/comm-central/rev/ba9e556dd988305051cd54ba1c6c13176e797f61/mail/test/mozmill/composition/test-cp932-display.js#6
EDIT: No, this one still passes.
| Assignee | ||
Updated•7 years ago
|
| Assignee | ||
Comment 3•7 years ago
|
||
Henri, can we use the new mozilla::JapaneseDetector to detect the various charsets. As you know, without specialised detection, ISO-2022-JP is detected as UTF-8 since it's 7bit :-( - If so, could you provide a code snippet for us.
| Assignee | ||
Comment 4•7 years ago
•
|
||
Wow, we have another SHift_JIS test which fails now :-(
TEST-UNEXPECTED-FAIL | comm/mailnews/import/test/unit/test_shiftjis_csv.js
https://searchfox.org/comm-central/rev/ba9e556dd988305051cd54ba1c6c13176e797f61/mailnews/import/test/unit/test_shiftjis_csv.js#4
Comment 6•7 years ago
|
||
(In reply to Jorg K (GMT+2) from comment #3)
Henri, can we use the new
mozilla::JapaneseDetectorto detect the various charsets. As you know, without specialised detection, ISO-2022-JP is detected as UTF-8 since it's 7bit :-( - If so, could you provide a code snippet for us.
In https://searchfox.org/comm-central/source/mailnews/base/util/nsMsgUtils.cpp#1839 in place of
// Use detector.
nsCOMPtr<nsICharsetDetector> detector;
nsAutoCString detectorName;
Preferences::GetLocalizedCString("intl.charset.detector", detectorName);
if (!detectorName.IsEmpty()) {
// We recognize one of the three magic strings for the following languages.
if (detectorName.EqualsLiteral("ruprob")) {
detector = new nsRUProbDetector();
} else if (detectorName.EqualsLiteral("ukprob")) {
detector = new nsUKProbDetector();
} else if (detectorName.EqualsLiteral("ja_parallel_state_machine")) {
detector = new nsJAPSMDetector();
}
}
if (detector) {
RefPtr<CharsetDetectionObserver> observer = new CharsetDetectionObserver();
rv = detector->Init(observer);
NS_ENSURE_SUCCESS(rv, rv);
char buffer[1024];
uint32_t numRead = 0;
bool dontFeed = false;
while (NS_SUCCEEDED(inputStream->Read(buffer, sizeof(buffer), &numRead))) {
// XXX: We need to break early here to work around a problem in Shift-JIS
// detection. If we call `DoIt()` with any empty buffer, Shift-JIS is not
// detected, however ISO-2022-JP is detected.
if (numRead == 0) break;
detector->DoIt(buffer, numRead, &dontFeed);
NS_ENSURE_SUCCESS(rv, rv);
if (dontFeed) // XXX: We should really break here with:
// if (dontFeed || numRead == 0).
break;
}
rv = detector->Done();
NS_ENSURE_SUCCESS(rv, rv);
observer->GetDetectedCharset(aCharset);
}
Instead something along the lines of:
// Use detector.
nsCOMPtr<nsICharsetDetector> detector;
mozilla::UniquePtr<mozilla::JapaneseDetector> japaneseDetector;
nsAutoCString detectorName;
Preferences::GetLocalizedCString("intl.charset.detector", detectorName);
if (!detectorName.IsEmpty()) {
// We recognize one of the three magic strings for the following languages.
if (detectorName.EqualsLiteral("ruprob")) {
detector = new nsRUProbDetector();
} else if (detectorName.EqualsLiteral("ukprob")) {
detector = new nsUKProbDetector();
} else if (detectorName.EqualsLiteral("ja_parallel_state_machine")) {
japaneseDetector = mozilla::JapaneseDetector::Create(true);
}
}
if (detector) {
RefPtr<CharsetDetectionObserver> observer = new CharsetDetectionObserver();
rv = detector->Init(observer);
NS_ENSURE_SUCCESS(rv, rv);
char buffer[1024];
uint32_t numRead = 0;
bool dontFeed = false;
while (NS_SUCCEEDED(inputStream->Read(buffer, sizeof(buffer), &numRead))) {
// XXX: We need to break early here to work around a problem in Shift-JIS
// detection. If we call `DoIt()` with any empty buffer, Shift-JIS is not
// detected, however ISO-2022-JP is detected.
if (numRead == 0) break;
detector->DoIt(buffer, numRead, &dontFeed);
NS_ENSURE_SUCCESS(rv, rv);
if (dontFeed) // XXX: We should really break here with:
// if (dontFeed || numRead == 0).
break;
}
rv = detector->Done();
NS_ENSURE_SUCCESS(rv, rv);
observer->GetDetectedCharset(aCharset);
} else if (japaneseDetector) {
char buffer[1024];
uint32_t numRead = 0;
bool dontFeed = false;
while (NS_SUCCEEDED(inputStream->Read(buffer, sizeof(buffer), &numRead))) {
mozilla::Span src = mozilla::AsBytes(mozilla::MakeSpan(buffer, numRead));
auto encoding = japaneseDetector->Feed(src, (numRead == 0));
if (encoding) {
encoding->Name(aCharset);
break;
}
if (numRead == 0) {
break;
}
}
In https://searchfox.org/comm-central/source/mailnews/mime/src/comi18n.cpp#69 if you want to keep detecting UTF-8 in the Japanese case, you need to add a UTF-8 check but let ISO-2022-JP take precedence.
Comment 7•7 years ago
|
||
| Assignee | ||
Comment 8•7 years ago
|
||
Like this? The auto encoding = japaneseDetector->Feed(src, false); is correct with the false, right?
Both tests pass again, even without the hunk in comi18n.cpp.
| Assignee | ||
Comment 9•7 years ago
•
|
||
How about this alternative version, it adds UTF-8 detection to MIME_detect_charset. That makes sense, no?
EDIT: https://bugzilla.mozilla.org/attachment.cgi?oldid=9069591&action=interdiff&newid=9069610&headers=1
Comment 10•7 years ago
|
||
Comment 11•7 years ago
|
||
Comment 12•7 years ago
|
||
(In reply to Jorg K (GMT+2) from comment #9)
How about this alternative version, it adds UTF-8 detection to
MIME_detect_charset. That makes sense, no?
Yeah, the second patch looks better.
| Assignee | ||
Comment 13•7 years ago
|
||
Thanks, Henri. I never posted the comment about my try run ... Try for the second version that changes the behaviour of MIME_detect_charset:
https://treeherder.mozilla.org/#/jobs?repo=try-comm-central&revision=a915ebf3bb68afca2d08f850fe452a18963f404b
It came out green, so let's take the second version.
| Assignee | ||
Comment 14•7 years ago
|
||
I added the braces and switched to MakeSpan(). Good to go.
| Assignee | ||
Updated•7 years ago
|
Comment 15•7 years ago
|
||
(In reply to Jorg K (GMT+2) from comment #8)
Like this? The
auto encoding = japaneseDetector->Feed(src, false);is correct with thefalse, right?
I missed this question earlier, sorry.
Passing false unconditionally is technically incorrect if the entire stream rather than just a prefix is examined. The only case that I can think of where it might make a difference is if there's a three-byte EUC-JP sequence so that the EUC-JP and Shift_JIS no longer agree on which pairs of bytes map to characters.
| Assignee | ||
Comment 16•7 years ago
|
||
This is not examining a stream, but a single string passed in. So it's a single call and aLast should be passed in as true right?
Comment 17•7 years ago
|
||
(In reply to Jorg K (GMT+2) from comment #16)
This is not examining a stream, but a single string passed in. So it's a single call and
aLastshould be passed in astrueright?
Yes.
| Assignee | ||
Comment 18•7 years ago
|
||
With true.
Comment 19•7 years ago
|
||
Pushed by mozilla@jorgk.com:
https://hg.mozilla.org/comm-central/rev/dda6d487c69a
Fix Japanese detection and re-enable tests. r=hsivonen
| Assignee | ||
Updated•7 years ago
|
Updated•6 years ago
|
Description
•