HexEditor.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "HexEditor.h"
  27. #include <AK/Debug.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibGUI/Action.h>
  30. #include <LibGUI/Clipboard.h>
  31. #include <LibGUI/Menu.h>
  32. #include <LibGUI/Painter.h>
  33. #include <LibGUI/Scrollbar.h>
  34. #include <LibGUI/TextEditor.h>
  35. #include <LibGUI/Window.h>
  36. #include <LibGfx/FontDatabase.h>
  37. #include <LibGfx/Palette.h>
  38. #include <ctype.h>
  39. #include <fcntl.h>
  40. #include <stdio.h>
  41. #include <unistd.h>
  42. HexEditor::HexEditor()
  43. {
  44. set_should_hide_unnecessary_scrollbars(true);
  45. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  46. set_scrollbars_enabled(true);
  47. set_font(Gfx::FontDatabase::default_fixed_width_font());
  48. set_background_role(ColorRole::Base);
  49. set_foreground_role(ColorRole::BaseText);
  50. vertical_scrollbar().set_step(line_height());
  51. }
  52. HexEditor::~HexEditor()
  53. {
  54. }
  55. void HexEditor::set_readonly(bool readonly)
  56. {
  57. if (m_readonly == readonly)
  58. return;
  59. m_readonly = readonly;
  60. }
  61. void HexEditor::set_buffer(const ByteBuffer& buffer)
  62. {
  63. m_buffer = buffer;
  64. set_content_length(buffer.size());
  65. m_tracked_changes.clear();
  66. m_position = 0;
  67. m_byte_position = 0;
  68. update();
  69. update_status();
  70. }
  71. void HexEditor::fill_selection(u8 fill_byte)
  72. {
  73. if (!has_selection())
  74. return;
  75. for (int i = m_selection_start; i <= m_selection_end; i++) {
  76. m_tracked_changes.set(i, m_buffer.data()[i]);
  77. m_buffer.data()[i] = fill_byte;
  78. }
  79. update();
  80. did_change();
  81. }
  82. void HexEditor::set_position(int position)
  83. {
  84. if (position > static_cast<int>(m_buffer.size()))
  85. return;
  86. m_position = position;
  87. m_byte_position = 0;
  88. scroll_position_into_view(position);
  89. update_status();
  90. }
  91. bool HexEditor::write_to_file(const String& path)
  92. {
  93. if (m_buffer.is_empty())
  94. return true;
  95. int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
  96. if (fd < 0) {
  97. perror("open");
  98. return false;
  99. }
  100. int rc = ftruncate(fd, m_buffer.size());
  101. if (rc < 0) {
  102. perror("ftruncate");
  103. return false;
  104. }
  105. ssize_t nwritten = write(fd, m_buffer.data(), m_buffer.size());
  106. if (nwritten < 0) {
  107. perror("write");
  108. close(fd);
  109. return false;
  110. }
  111. if (static_cast<size_t>(nwritten) == m_buffer.size()) {
  112. m_tracked_changes.clear();
  113. update();
  114. }
  115. close(fd);
  116. return true;
  117. }
  118. bool HexEditor::copy_selected_hex_to_clipboard()
  119. {
  120. if (!has_selection())
  121. return false;
  122. StringBuilder output_string_builder;
  123. for (int i = m_selection_start; i <= m_selection_end; i++)
  124. output_string_builder.appendff("{:02X} ", m_buffer.data()[i]);
  125. GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
  126. return true;
  127. }
  128. bool HexEditor::copy_selected_text_to_clipboard()
  129. {
  130. if (!has_selection())
  131. return false;
  132. StringBuilder output_string_builder;
  133. for (int i = m_selection_start; i <= m_selection_end; i++)
  134. output_string_builder.append(isprint(m_buffer.data()[i]) ? m_buffer[i] : '.');
  135. GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
  136. return true;
  137. }
  138. bool HexEditor::copy_selected_hex_to_clipboard_as_c_code()
  139. {
  140. if (!has_selection())
  141. return false;
  142. StringBuilder output_string_builder;
  143. output_string_builder.appendff("unsigned char raw_data[{}] = {{\n", (m_selection_end - m_selection_start) + 1);
  144. output_string_builder.append(" ");
  145. for (int i = m_selection_start, j = 1; i <= m_selection_end; i++, j++) {
  146. output_string_builder.appendff("{:#02X}", m_buffer.data()[i]);
  147. if (i != m_selection_end)
  148. output_string_builder.append(", ");
  149. if ((j % 12) == 0) {
  150. output_string_builder.append("\n");
  151. output_string_builder.append(" ");
  152. }
  153. }
  154. output_string_builder.append("\n};\n");
  155. GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
  156. return true;
  157. }
  158. void HexEditor::set_bytes_per_row(int bytes_per_row)
  159. {
  160. m_bytes_per_row = bytes_per_row;
  161. set_content_size({ offset_margin_width() + (m_bytes_per_row * (character_width() * 3)) + 10 + (m_bytes_per_row * character_width()) + 20, total_rows() * line_height() + 10 });
  162. update();
  163. }
  164. void HexEditor::set_content_length(int length)
  165. {
  166. if (length == m_content_length)
  167. return;
  168. m_content_length = length;
  169. set_content_size({ offset_margin_width() + (m_bytes_per_row * (character_width() * 3)) + 10 + (m_bytes_per_row * character_width()) + 20, total_rows() * line_height() + 10 });
  170. }
  171. void HexEditor::mousedown_event(GUI::MouseEvent& event)
  172. {
  173. if (event.button() != GUI::MouseButton::Left) {
  174. return;
  175. }
  176. auto absolute_x = horizontal_scrollbar().value() + event.x();
  177. auto absolute_y = vertical_scrollbar().value() + event.y();
  178. auto hex_start_x = frame_thickness() + 90;
  179. auto hex_start_y = frame_thickness() + 5;
  180. auto hex_end_x = hex_start_x + (bytes_per_row() * (character_width() * 3));
  181. auto hex_end_y = hex_start_y + 5 + (total_rows() * line_height());
  182. auto text_start_x = frame_thickness() + 100 + (bytes_per_row() * (character_width() * 3));
  183. auto text_start_y = frame_thickness() + 5;
  184. auto text_end_x = text_start_x + (bytes_per_row() * character_width());
  185. auto text_end_y = text_start_y + 5 + (total_rows() * line_height());
  186. if (absolute_x >= hex_start_x && absolute_x <= hex_end_x && absolute_y >= hex_start_y && absolute_y <= hex_end_y) {
  187. auto byte_x = (absolute_x - hex_start_x) / (character_width() * 3);
  188. auto byte_y = (absolute_y - hex_start_y) / line_height();
  189. auto offset = (byte_y * m_bytes_per_row) + byte_x;
  190. if (offset < 0 || offset >= static_cast<int>(m_buffer.size()))
  191. return;
  192. dbgln_if(HEX_DEBUG, "HexEditor::mousedown_event(hex): offset={}", offset);
  193. m_edit_mode = EditMode::Hex;
  194. m_byte_position = 0;
  195. m_position = offset;
  196. m_in_drag_select = true;
  197. m_selection_start = offset;
  198. m_selection_end = offset;
  199. update();
  200. update_status();
  201. }
  202. if (absolute_x >= text_start_x && absolute_x <= text_end_x && absolute_y >= text_start_y && absolute_y <= text_end_y) {
  203. auto byte_x = (absolute_x - text_start_x) / character_width();
  204. auto byte_y = (absolute_y - text_start_y) / line_height();
  205. auto offset = (byte_y * m_bytes_per_row) + byte_x;
  206. if (offset < 0 || offset >= static_cast<int>(m_buffer.size()))
  207. return;
  208. dbgln_if(HEX_DEBUG, "HexEditor::mousedown_event(text): offset={}", offset);
  209. m_position = offset;
  210. m_byte_position = 0;
  211. m_in_drag_select = true;
  212. m_selection_start = offset;
  213. m_selection_end = offset;
  214. m_edit_mode = EditMode::Text;
  215. update();
  216. update_status();
  217. }
  218. }
  219. void HexEditor::mousemove_event(GUI::MouseEvent& event)
  220. {
  221. auto absolute_x = horizontal_scrollbar().value() + event.x();
  222. auto absolute_y = vertical_scrollbar().value() + event.y();
  223. auto hex_start_x = frame_thickness() + 90;
  224. auto hex_start_y = frame_thickness() + 5;
  225. auto hex_end_x = hex_start_x + (bytes_per_row() * (character_width() * 3));
  226. auto hex_end_y = hex_start_y + 5 + (total_rows() * line_height());
  227. auto text_start_x = frame_thickness() + 100 + (bytes_per_row() * (character_width() * 3));
  228. auto text_start_y = frame_thickness() + 5;
  229. auto text_end_x = text_start_x + (bytes_per_row() * character_width());
  230. auto text_end_y = text_start_y + 5 + (total_rows() * line_height());
  231. if ((absolute_x >= hex_start_x && absolute_x <= hex_end_x
  232. && absolute_y >= hex_start_y && absolute_y <= hex_end_y)
  233. || (absolute_x >= text_start_x && absolute_x <= text_end_x
  234. && absolute_y >= text_start_y && absolute_y <= text_end_y)) {
  235. set_override_cursor(Gfx::StandardCursor::IBeam);
  236. } else {
  237. set_override_cursor(Gfx::StandardCursor::None);
  238. }
  239. if (m_in_drag_select) {
  240. if (absolute_x >= hex_start_x && absolute_x <= hex_end_x && absolute_y >= hex_start_y && absolute_y <= hex_end_y) {
  241. auto byte_x = (absolute_x - hex_start_x) / (character_width() * 3);
  242. auto byte_y = (absolute_y - hex_start_y) / line_height();
  243. auto offset = (byte_y * m_bytes_per_row) + byte_x;
  244. if (offset < 0 || offset > static_cast<int>(m_buffer.size()))
  245. return;
  246. m_selection_end = offset;
  247. scroll_position_into_view(offset);
  248. }
  249. if (absolute_x >= text_start_x && absolute_x <= text_end_x && absolute_y >= text_start_y && absolute_y <= text_end_y) {
  250. auto byte_x = (absolute_x - text_start_x) / character_width();
  251. auto byte_y = (absolute_y - text_start_y) / line_height();
  252. auto offset = (byte_y * m_bytes_per_row) + byte_x;
  253. if (offset < 0 || offset > static_cast<int>(m_buffer.size()))
  254. return;
  255. m_selection_end = offset;
  256. scroll_position_into_view(offset);
  257. }
  258. update_status();
  259. update();
  260. return;
  261. }
  262. }
  263. void HexEditor::mouseup_event(GUI::MouseEvent& event)
  264. {
  265. if (event.button() == GUI::MouseButton::Left) {
  266. if (m_in_drag_select) {
  267. if (m_selection_end < m_selection_start) {
  268. // lets flip these around
  269. auto start = m_selection_end;
  270. m_selection_end = m_selection_start;
  271. m_selection_start = start;
  272. }
  273. m_in_drag_select = false;
  274. }
  275. update();
  276. update_status();
  277. }
  278. }
  279. void HexEditor::scroll_position_into_view(int position)
  280. {
  281. int y = position / bytes_per_row();
  282. int x = position % bytes_per_row();
  283. Gfx::IntRect rect {
  284. frame_thickness() + offset_margin_width() + (x * (character_width() * 3)) + 10,
  285. frame_thickness() + 5 + (y * line_height()),
  286. (character_width() * 3),
  287. line_height() - m_line_spacing
  288. };
  289. scroll_into_view(rect, true, true);
  290. }
  291. void HexEditor::keydown_event(GUI::KeyEvent& event)
  292. {
  293. dbgln_if(HEX_DEBUG, "HexEditor::keydown_event key={}", static_cast<u8>(event.key()));
  294. if (event.key() == KeyCode::Key_Up) {
  295. if (m_position - bytes_per_row() >= 0) {
  296. m_position -= bytes_per_row();
  297. m_byte_position = 0;
  298. scroll_position_into_view(m_position);
  299. update();
  300. update_status();
  301. }
  302. return;
  303. }
  304. if (event.key() == KeyCode::Key_Down) {
  305. if (m_position + bytes_per_row() < static_cast<int>(m_buffer.size())) {
  306. m_position += bytes_per_row();
  307. m_byte_position = 0;
  308. scroll_position_into_view(m_position);
  309. update();
  310. update_status();
  311. }
  312. return;
  313. }
  314. if (event.key() == KeyCode::Key_Left) {
  315. if (m_position - 1 >= 0) {
  316. m_position--;
  317. m_byte_position = 0;
  318. scroll_position_into_view(m_position);
  319. update();
  320. update_status();
  321. }
  322. return;
  323. }
  324. if (event.key() == KeyCode::Key_Right) {
  325. if (m_position + 1 < static_cast<int>(m_buffer.size())) {
  326. m_position++;
  327. m_byte_position = 0;
  328. scroll_position_into_view(m_position);
  329. update();
  330. update_status();
  331. }
  332. return;
  333. }
  334. if (event.key() == KeyCode::Key_Backspace) {
  335. if (m_position > 0) {
  336. m_position--;
  337. m_byte_position = 0;
  338. scroll_position_into_view(m_position);
  339. update();
  340. update_status();
  341. }
  342. return;
  343. }
  344. if (!is_readonly() && !event.ctrl() && !event.alt() && !event.text().is_empty()) {
  345. if (m_edit_mode == EditMode::Hex) {
  346. hex_mode_keydown_event(event);
  347. } else {
  348. text_mode_keydown_event(event);
  349. }
  350. }
  351. }
  352. void HexEditor::hex_mode_keydown_event(GUI::KeyEvent& event)
  353. {
  354. if ((event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9) || (event.key() >= KeyCode::Key_A && event.key() <= KeyCode::Key_F)) {
  355. if (m_buffer.is_empty())
  356. return;
  357. VERIFY(m_position >= 0);
  358. VERIFY(m_position < static_cast<int>(m_buffer.size()));
  359. // yes, this is terrible... but it works.
  360. auto value = (event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9)
  361. ? event.key() - KeyCode::Key_0
  362. : (event.key() - KeyCode::Key_A) + 0xA;
  363. if (m_byte_position == 0) {
  364. m_tracked_changes.set(m_position, m_buffer.data()[m_position]);
  365. m_buffer.data()[m_position] = value << 4 | (m_buffer.data()[m_position] & 0xF); // shift new value left 4 bits, OR with existing last 4 bits
  366. m_byte_position++;
  367. } else {
  368. m_buffer.data()[m_position] = (m_buffer.data()[m_position] & 0xF0) | value; // save the first 4 bits, OR the new value in the last 4
  369. if (m_position + 1 < static_cast<int>(m_buffer.size()))
  370. m_position++;
  371. m_byte_position = 0;
  372. }
  373. update();
  374. update_status();
  375. did_change();
  376. }
  377. }
  378. void HexEditor::text_mode_keydown_event(GUI::KeyEvent& event)
  379. {
  380. if (m_buffer.is_empty())
  381. return;
  382. VERIFY(m_position >= 0);
  383. VERIFY(m_position < static_cast<int>(m_buffer.size()));
  384. if (event.code_point() == 0) // This is a control key
  385. return;
  386. m_tracked_changes.set(m_position, m_buffer.data()[m_position]);
  387. m_buffer.data()[m_position] = event.code_point();
  388. if (m_position + 1 < static_cast<int>(m_buffer.size()))
  389. m_position++;
  390. m_byte_position = 0;
  391. update();
  392. update_status();
  393. did_change();
  394. }
  395. void HexEditor::update_status()
  396. {
  397. if (on_status_change)
  398. on_status_change(m_position, m_edit_mode, m_selection_start, m_selection_end);
  399. }
  400. void HexEditor::did_change()
  401. {
  402. if (on_change)
  403. on_change();
  404. }
  405. void HexEditor::paint_event(GUI::PaintEvent& event)
  406. {
  407. GUI::Frame::paint_event(event);
  408. GUI::Painter painter(*this);
  409. painter.add_clip_rect(widget_inner_rect());
  410. painter.add_clip_rect(event.rect());
  411. painter.fill_rect(event.rect(), palette().color(background_role()));
  412. if (m_buffer.is_empty())
  413. return;
  414. painter.translate(frame_thickness(), frame_thickness());
  415. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  416. Gfx::IntRect offset_clip_rect {
  417. 0,
  418. vertical_scrollbar().value(),
  419. 85,
  420. height() - height_occupied_by_horizontal_scrollbar() //(total_rows() * line_height()) + 5
  421. };
  422. painter.fill_rect(offset_clip_rect, palette().ruler());
  423. painter.draw_line(offset_clip_rect.top_right(), offset_clip_rect.bottom_right(), palette().ruler_border());
  424. auto margin_and_hex_width = offset_margin_width() + (m_bytes_per_row * (character_width() * 3)) + 15;
  425. painter.draw_line({ margin_and_hex_width, 0 },
  426. { margin_and_hex_width, vertical_scrollbar().value() + (height() - height_occupied_by_horizontal_scrollbar()) },
  427. palette().ruler_border());
  428. auto view_height = (height() - height_occupied_by_horizontal_scrollbar());
  429. auto min_row = max(0, vertical_scrollbar().value() / line_height()); // if below 0 then use 0
  430. auto max_row = min(total_rows(), min_row + ceil_div(view_height, line_height())); // if above calculated rows, use calculated rows
  431. // paint offsets
  432. for (int i = min_row; i < max_row; i++) {
  433. Gfx::IntRect side_offset_rect {
  434. frame_thickness() + 5,
  435. frame_thickness() + 5 + (i * line_height()),
  436. width() - width_occupied_by_vertical_scrollbar(),
  437. height() - height_occupied_by_horizontal_scrollbar()
  438. };
  439. bool is_current_line = (m_position / bytes_per_row()) == i;
  440. auto line = String::formatted("{:#08X}", i * bytes_per_row());
  441. painter.draw_text(
  442. side_offset_rect,
  443. line,
  444. is_current_line ? Gfx::FontDatabase::default_bold_font() : font(),
  445. Gfx::TextAlignment::TopLeft,
  446. is_current_line ? palette().ruler_active_text() : palette().ruler_inactive_text());
  447. }
  448. for (int i = min_row; i < max_row; i++) {
  449. for (int j = 0; j < bytes_per_row(); j++) {
  450. auto byte_position = (i * bytes_per_row()) + j;
  451. if (byte_position >= static_cast<int>(m_buffer.size()))
  452. return;
  453. Color text_color = palette().color(foreground_role());
  454. if (m_tracked_changes.contains(byte_position)) {
  455. text_color = Color::Red;
  456. }
  457. auto highlight_flag = false;
  458. if (m_selection_start > -1 && m_selection_end > -1) {
  459. if (byte_position >= m_selection_start && byte_position <= m_selection_end) {
  460. highlight_flag = true;
  461. }
  462. if (byte_position >= m_selection_end && byte_position <= m_selection_start) {
  463. highlight_flag = true;
  464. }
  465. }
  466. Gfx::IntRect hex_display_rect {
  467. frame_thickness() + offset_margin_width() + (j * (character_width() * 3)) + 10,
  468. frame_thickness() + 5 + (i * line_height()),
  469. (character_width() * 3),
  470. line_height() - m_line_spacing
  471. };
  472. if (highlight_flag) {
  473. painter.fill_rect(hex_display_rect, palette().selection());
  474. text_color = text_color == Color::Red ? Color::from_rgb(0xFFC0CB) : palette().selection_text();
  475. } else if (byte_position == m_position) {
  476. painter.fill_rect(hex_display_rect, palette().inactive_selection());
  477. text_color = palette().inactive_selection_text();
  478. }
  479. auto line = String::formatted("{:02X}", m_buffer[byte_position]);
  480. painter.draw_text(hex_display_rect, line, Gfx::TextAlignment::TopLeft, text_color);
  481. Gfx::IntRect text_display_rect {
  482. frame_thickness() + offset_margin_width() + (bytes_per_row() * (character_width() * 3)) + (j * character_width()) + 20,
  483. frame_thickness() + 5 + (i * line_height()),
  484. character_width(),
  485. line_height() - m_line_spacing
  486. };
  487. // selection highlighting.
  488. if (highlight_flag) {
  489. painter.fill_rect(text_display_rect, palette().selection());
  490. } else if (byte_position == m_position) {
  491. painter.fill_rect(text_display_rect, palette().inactive_selection());
  492. }
  493. painter.draw_text(text_display_rect, String::formatted("{:c}", isprint(m_buffer[byte_position]) ? m_buffer[byte_position] : '.'), Gfx::TextAlignment::TopLeft, text_color);
  494. }
  495. }
  496. }
  497. int HexEditor::find_and_highlight(ByteBuffer& needle, int start)
  498. {
  499. if (m_buffer.is_empty())
  500. return -1;
  501. if (needle.is_null()) {
  502. dbgln("needle is null");
  503. return -1;
  504. }
  505. auto raw_offset = memmem(m_buffer.data() + start, m_buffer.size(), needle.data(), needle.size());
  506. if (raw_offset == NULL)
  507. return -1;
  508. int relative_offset = static_cast<const u8*>(raw_offset) - m_buffer.data();
  509. dbgln("find_and_highlight: start={} raw_offset={} relative_offset={}", start, raw_offset, relative_offset);
  510. auto end_of_match = relative_offset + needle;
  511. set_position(relative_offset);
  512. m_selection_start = relative_offset;
  513. m_selection_end = end_of_match;
  514. return end_of_match;
  515. }