Skip to content

Commit f6a34f4

Browse files
committed
[textfile] render invalid utf
1 parent 16773d5 commit f6a34f4

5 files changed

Lines changed: 35 additions & 21 deletions

File tree

src/log_format_fwd.hh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@ struct subline_options {
487487
const subline_options& rhs)
488488
{
489489
return lhs.full_message == rhs.full_message
490-
&& lhs.hash_hack == rhs.hash_hack;
490+
&& lhs.hash_hack == rhs.hash_hack
491+
&& lhs.scrub_invalid_utf8 == rhs.scrub_invalid_utf8;
491492
}
492493

493494
friend bool operator!=(const subline_options& lhs,
@@ -498,6 +499,7 @@ struct subline_options {
498499

499500
bool full_message{false};
500501
bool hash_hack{false};
502+
bool scrub_invalid_utf8{true};
501503
};
502504

503505
enum class value_kind_t : int {

src/logfile.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,9 @@ logfile::read_line(iterator ll, subline_options opts)
19831983
return this->lf_line_buffer.read_range(get_range_res)
19841984
.map([&ll, &get_range_res, &opts, this](auto sbr) {
19851985
sbr.rtrim(is_line_ending);
1986-
if (!get_range_res.fr_metadata.m_valid_utf) {
1986+
if (opts.scrub_invalid_utf8
1987+
&& !get_range_res.fr_metadata.m_valid_utf)
1988+
{
19871989
scrub_to_utf8(sbr.get_writable_data(), sbr.length());
19881990
sbr.get_metadata().m_valid_utf = true;
19891991
}

src/logfile.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public:
286286

287287
bool is_closed() const { return this->lf_is_closed; }
288288

289-
struct timeval original_line_time(iterator ll);
289+
timeval original_line_time(iterator ll);
290290

291291
Result<shared_buffer_ref, std::string> read_line(iterator ll,
292292
subline_options opts = {});

src/textfile_sub_source.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,16 @@ textfile_sub_source::text_value_for_line(textview_curses& tc,
173173
}
174174

175175
const auto ll = lf->begin() + lfo->lfo_filter_state.tfs_index[line];
176-
auto read_result = lf->read_line(ll);
176+
auto read_opts = subline_options{};
177+
read_opts.scrub_invalid_utf8 = false;
178+
auto read_result = lf->read_line(ll, read_opts);
177179
this->tss_line_indent_size = 0;
178180
this->tss_plain_line_attrs.clear();
179181
if (read_result.isOk()) {
180182
auto sbr = read_result.unwrap();
181183
value_out = to_string(sbr);
182-
if (sbr.get_metadata().m_has_ansi) {
184+
const auto& meta = sbr.get_metadata();
185+
if (meta.m_valid_utf && meta.m_has_ansi) {
183186
scrub_ansi_string(value_out, &this->tss_plain_line_attrs);
184187
}
185188
for (const auto& ch : value_out) {
@@ -852,7 +855,9 @@ textfile_sub_source::rescan_files(textfile_sub_source::scan_callback& callback,
852855
"discovery",
853856
lf->get_path_for_key().c_str());
854857
iter->fvs_mtime = st.st_mtime;
855-
iter->fvs_file_size = lf->get_index_size();
858+
iter->fvs_file_size = st.st_size;
859+
iter->fvs_file_indexed_size = lf->get_index_size();
860+
iter->fvs_error = "skipping meta discovery";
856861
} else {
857862
auto content
858863
= attr_line_t(read_file_res.rfr_content);

src/view_curses.cc

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "shlex.hh"
5151
#include "terminfo-files.h"
5252
#include "terminfo/terminfo.h"
53+
#include "unistr.h"
5354
#include "uniwidth.h"
5455
#include "xterm_mouse.hh"
5556

@@ -365,25 +366,29 @@ view_curses::mvwattrline(ncplane* window,
365366
break;
366367
}
367368

368-
auto exp_read_start = expanded_line.size();
369+
if (ch <= 0x7f) {
370+
expanded_line.push_back(ch);
371+
curr_ch_col_count = 1;
372+
char_index += 1;
373+
lpc += 1;
374+
break;
375+
}
376+
369377
auto lpc_start = lpc;
370-
auto read_res
371-
= ww898::utf::utf8::read([&line, &expanded_line, &lpc] {
372-
auto ch = line[lpc++];
373-
expanded_line.push_back(ch);
374-
return ch;
375-
});
376-
377-
if (read_res.isErr()) {
378-
log_trace(
379-
"error:%d:%zu:%s", y, x + lpc, read_res.unwrapErr());
380-
expanded_line.resize(exp_read_start);
381-
expanded_line.push_back('?');
378+
ucs4_t wch;
379+
auto read_res = u8_mbtoucr(
380+
&wch, (uint8_t*) line.data() + lpc, line.size() - lpc);
381+
if (read_res <= 0) {
382+
expanded_line.append("\ufffd");
383+
sa.emplace_back(line_range{(int) lpc, (int) lpc + 1},
384+
VC_ROLE.value(role_t::VCR_NON_ASCII));
382385
curr_ch_col_count = 1;
383386
char_index += 1;
384-
lpc = lpc_start + 1;
387+
lpc += 1;
385388
} else {
386-
auto wch = read_res.unwrap();
389+
for (size_t i = 0; i < read_res; i++) {
390+
expanded_line.push_back(line[lpc++]);
391+
}
387392
if (wch == L'\u200d') {
388393
join_start_index = char_index - last_ch_col_count;
389394
continue;

0 commit comments

Comments
 (0)