-
Notifications
You must be signed in to change notification settings - Fork 576
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix: under windows, librime doesn't support resource_id(file name) with non-ANSI characters, which is supported by code page. #804
Closed
Conversation
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
…iled in some cases.
@lotem here is a sample test #include <iostream>
#include <string>
#include <filesystem>
#include <windows.h>
static std::wstring U8ToU16(const std::string& str) {
std::wstring ret;
int length = static_cast<int>(str.length());
int convcnt = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, NULL, 0);
if (convcnt > 0) {
ret.resize(convcnt);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, &ret[0], convcnt);
}
return ret;
}
int main() {
// with a file name ς.txt in the same directory
// whereas "ς" is not a character supported by cp936
std::string xlfn = u8"ς";
std::cout << "file name no ext: " << xlfn << std::endl;
std::cout << "-----------------------------------------" << std::endl;
// path with wstirng
std::wstring xlfnw = U8ToU16(xlfn);
std::filesystem::path xlpathw(U8ToU16(".\\") + xlfnw + U8ToU16(".txt"));
bool chk = std::filesystem::exists(xlpathw);
std::cout << "path by wstring, file exists ? " << (chk ? "yes" : "no") << std::endl;
std::cout << "-----------------------------------------" << std::endl;
// path with stirng
std::filesystem::path xlpath(".\\" + xlfn + ".txt");
chk = std::filesystem::exists(xlpath);
std::cout << "path by string, file exists ? " << (chk ? "yes" : "no") << std::endl;
return 0;
}
If acceptable, the final solution would be( to be tested ) update: failed #ifdef _MSC_VER
static std::wstring U8ToU16(const std::string& str) {
std::wstring ret;
int length = static_cast<int>(str.length());
int convcnt = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, NULL, 0);
if (convcnt > 0) {
ret.resize(convcnt);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, &ret[0], convcnt);
}
return ret;
}
#endif /* _MSC_VER*/
std::filesystem::path ResourceResolver::ResolvePath(const string& resource_id) {
#ifdef _MSC_VER
// use std::wstring, avoiding codepage issue
return std::filesystem::absolute(
root_path_ /
std::filesystem::path(U8ToU16(type_.prefix) + U8ToU16(resource_id) +
U8ToU16(type_.suffix)));
#else
return std::filesystem::absolute(
root_path_ /
std::filesystem::path(type_.prefix + resource_id + type_.suffix));
#endif
}
std::filesystem::path FallbackResourceResolver::ResolvePath(
const string& resource_id) {
auto default_path = ResourceResolver::ResolvePath(resource_id);
if (!std::filesystem::exists(default_path)) {
#ifdef _MSC_VER
// use std::wstring, avoiding codepage issue
auto fallback_path = std::filesystem::absolute(
fallback_root_path_ /
std::filesystem::path(U8ToU16(type_.prefix) + U8ToU16(resource_id) +
U8ToU16(type_.suffix)));
#else
auto fallback_path = std::filesystem::absolute(
fallback_root_path_ /
std::filesystem::path(type_.prefix + resource_id + type_.suffix));
#endif
if (std::filesystem::exists(fallback_path)) {
return fallback_path;
}
}
return default_path;
}
|
lotem
added a commit
to lotem/librime
that referenced
this pull request
Feb 3, 2024
Follow @fxliang 's PR, use `u8path` on Windows to convert UTF-8 string to Windows native path. Closes rime#804 Fixes rime/weasel#576 Fixes rime/weasel#1080 BREAKING CHANGE: `installation.yaml` should be UTF-8 encoded. Previouly on Windows, the file can be written in local encoding to enable paths with non-ASCII characters. It should be updated to UTF-8 after this change.
2 tasks
lotem
added a commit
to lotem/librime
that referenced
this pull request
Feb 3, 2024
Follow @fxliang 's PR, use `u8path` on Windows to convert UTF-8 string to Windows native path. Closes rime#804 Fixes rime/weasel#576 Fixes rime/weasel#1080 BREAKING CHANGE: `installation.yaml` should be UTF-8 encoded. Previouly on Windows, the file can be written in local encoding to enable paths with non-ASCII characters. It should be updated to UTF-8 after this change.
lotem
added a commit
to lotem/librime
that referenced
this pull request
Feb 3, 2024
Follow @fxliang 's PR, use `u8path` on Windows to convert UTF-8 string to Windows native path. Closes rime#804 Fixes rime/weasel#576 Fixes rime/weasel#1080 BREAKING CHANGE: `installation.yaml` should be UTF-8 encoded. Previouly on Windows, the file can be written in local encoding to enable paths with non-ASCII characters. It should be updated to UTF-8 after this change.
lotem
added a commit
to lotem/librime
that referenced
this pull request
Feb 3, 2024
Follow @fxliang 's PR, use `u8path` on Windows to convert UTF-8 string to Windows native path. Closes rime#804 Fixes rime/weasel#576 Fixes rime/weasel#1080 BREAKING CHANGE: `installation.yaml` should be UTF-8 encoded. Previouly on Windows, the file can be written in local encoding to enable paths with non-ASCII characters. It should be updated to UTF-8 after this change.
lotem
added a commit
to lotem/librime
that referenced
this pull request
Feb 3, 2024
Follow @fxliang 's PR, use `u8path` on Windows to convert UTF-8 string to Windows native path. Closes rime#804 Fixes rime/weasel#576 Fixes rime/weasel#1080 BREAKING CHANGE: `installation.yaml` should be UTF-8 encoded. Previouly on Windows, the file can be written in local encoding to enable paths with non-ASCII characters. It should be updated to UTF-8 after this change.
lotem
added a commit
to lotem/librime
that referenced
this pull request
Feb 4, 2024
Follow @fxliang 's PR, use `u8path` on Windows to convert UTF-8 string to Windows native path. Closes rime#804 Fixes rime/weasel#576 Fixes rime/weasel#1080 BREAKING CHANGE: `installation.yaml` should be UTF-8 encoded. Previouly on Windows, the file can be written in local encoding to enable paths with non-ASCII characters. It should be updated to UTF-8 after this change.
lotem
added a commit
that referenced
this pull request
Feb 6, 2024
refactor: convert path to native encoding on Windows feat(rime_api): provide secure version of path getter functions `RimeApi::get_*_dir_s`. Follow @fxliang 's PR, use `u8path` on Windows to convert UTF-8 string to Windows native path. Closes #804 Fixes rime/weasel#576 Fixes rime/weasel#1080 BREAKING CHANGE: Most `string` filenames in APIs are changed to `path`; `installation.yaml` should be UTF-8 encoded. Previouly on Windows, the file can be written in local encoding to enable paths with non-ASCII characters. It should be updated to UTF-8 after this change. Details of the code refactor Wrap `std::filesystem::path` in a thin wrapper class `rime::path` which calls `std::filesystem::u8path` in the constructor on Windows. Operator `/=` and `/` are also overloaded to convert the right operand from UTF-8 string to native path. Follow these rules to apply correct conversion between `string` and `rime::path`: - construct `rime::path` with UTF-8 encoded string; - get native string by `path::u8string`; - to extract UTF-8 string from `path`, for example to find schema ID from file name, call `path::u8string`; - avoid implicit conversion from string, which results in `std::filesystem::path` without performing UTF-8 to native conversion; - explicitly construct `rime::path` from `std::filesystem::path` before append operation, to ensure the overloaded operator with string conversion is used.
graphemecluster
pushed a commit
to TypeDuck-HK/librime
that referenced
this pull request
Mar 18, 2024
refactor: convert path to native encoding on Windows feat(rime_api): provide secure version of path getter functions `RimeApi::get_*_dir_s`. Follow @fxliang 's PR, use `u8path` on Windows to convert UTF-8 string to Windows native path. Closes rime#804 Fixes rime/weasel#576 Fixes rime/weasel#1080 BREAKING CHANGE: Most `string` filenames in APIs are changed to `path`; `installation.yaml` should be UTF-8 encoded. Previouly on Windows, the file can be written in local encoding to enable paths with non-ASCII characters. It should be updated to UTF-8 after this change. Details of the code refactor Wrap `std::filesystem::path` in a thin wrapper class `rime::path` which calls `std::filesystem::u8path` in the constructor on Windows. Operator `/=` and `/` are also overloaded to convert the right operand from UTF-8 string to native path. Follow these rules to apply correct conversion between `string` and `rime::path`: - construct `rime::path` with UTF-8 encoded string; - get native string by `path::u8string`; - to extract UTF-8 string from `path`, for example to find schema ID from file name, call `path::u8string`; - avoid implicit conversion from string, which results in `std::filesystem::path` without performing UTF-8 to native conversion; - explicitly construct `rime::path` from `std::filesystem::path` before append operation, to ensure the overloaded operator with string conversion is used.
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull request
Issue tracker
Fixes will automatically close the related issue
Fixes #
under windows, librime doesn't support resource_id(file name) with non-ANSI characters, which is supported by code page.
Feature
Describe feature of pull request
Unit test
Manual test
Code Review
Additional Info