Skip to content

Commit 363a73c

Browse files
Merge pull request #62 from Reim-developer/dev
Implement `utf-8` module and improve testing.
2 parents 0e8e7fb + a53cacc commit 363a73c

11 files changed

Lines changed: 714 additions & 18 deletions

File tree

src/back_end/src/core/macro.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[macro_export]
2+
macro_rules! ensure {
3+
($cond:expr, $err:expr) => {
4+
if !$cond {
5+
return $err;
6+
}
7+
};
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[derive(PartialEq, Eq, Debug)]
2+
#[repr(C)]
3+
#[allow(non_camel_case_types)]
4+
pub enum AllocResult {
5+
OK,
6+
NULL_DEFERENCE_ERR,
7+
C_STRING_CONVERT_ERR,
8+
}

src/back_end/src/core/sha.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
use std::ffi::{CStr, CString, c_char};
22

3+
use crate::core::result_enum::AllocResult;
4+
use crate::ensure;
35
use sha2::{Digest, Sha256};
46

5-
#[repr(C)]
6-
#[allow(non_camel_case_types)]
7-
pub enum GetShaResult {
8-
OK,
9-
C_STRING_CONVERT_ERR,
10-
}
11-
127
/// # Safety
138
/// Careful with raw pointers.
149
#[unsafe(no_mangle)]
1510
pub unsafe extern "C" fn text_sha256(
1611
text: *const c_char,
1712
out: *mut *mut c_char,
18-
) -> GetShaResult {
13+
) -> AllocResult {
1914
unsafe {
20-
use GetShaResult as R;
15+
use AllocResult as R;
16+
17+
ensure!(!text.is_null(), R::NULL_DEFERENCE_ERR);
18+
ensure!(!out.is_null(), R::NULL_DEFERENCE_ERR);
2119

2220
let mut hasher = Sha256::new();
2321

src/back_end/src/core/utf8.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::ffi::{CStr, CString, c_char};
2+
3+
use crate::{core::result_enum::AllocResult, ensure};
4+
5+
#[unsafe(no_mangle)]
6+
/// # Safety
7+
/// Careful with raw pointers & memory leaks
8+
pub unsafe extern "C" fn to_utf8(
9+
content: *const c_char,
10+
out: *mut *mut c_char,
11+
) -> AllocResult {
12+
use AllocResult as R;
13+
14+
unsafe {
15+
ensure!(!content.is_null(), R::NULL_DEFERENCE_ERR);
16+
ensure!(!content.is_null(), R::NULL_DEFERENCE_ERR);
17+
18+
let result_str = CStr::from_ptr(content).to_string_lossy();
19+
20+
let Ok(c_str) = CString::new(result_str.as_ref()) else {
21+
return R::C_STRING_CONVERT_ERR;
22+
};
23+
24+
*out = c_str.into_raw();
25+
26+
R::OK
27+
}
28+
}

src/back_end/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ pub mod utils {
77
}
88

99
pub mod core {
10+
pub mod r#macro;
11+
pub mod result_enum;
1012
pub mod sha;
1113
pub mod sqlite;
14+
pub mod utf8;
1215
}
1316

1417
pub mod internal {

src/back_end/tests/utf8.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#[test]
2+
/* Null deference beavior test. */
3+
fn test_utf8_null_dererence() {
4+
use back_end::core::result_enum::AllocResult;
5+
use back_end::core::utf8::to_utf8;
6+
use std::ffi::c_char;
7+
use std::ptr;
8+
9+
use AllocResult as R;
10+
11+
unsafe {
12+
let mut output_null: *mut c_char = ptr::null_mut();
13+
let result_null: *const c_char = ptr::null();
14+
15+
let result = to_utf8(result_null, &raw mut output_null);
16+
17+
assert_ne!(result, R::OK);
18+
assert_ne!(result, R::C_STRING_CONVERT_ERR);
19+
assert_eq!(result, R::NULL_DEFERENCE_ERR);
20+
}
21+
}
22+
23+
#[test]
24+
fn test_utf8() {
25+
unsafe {
26+
use back_end::core::result_enum::AllocResult;
27+
use back_end::core::utf8::to_utf8;
28+
use std::ffi::{CStr, CString, c_char};
29+
use std::ptr::null_mut;
30+
31+
use AllocResult as R;
32+
33+
let mut out: *mut c_char = null_mut();
34+
let content = CString::new(
35+
"café 世界 🌍\n春の海 ひねもすのたり のたりかな\nشكرا لك.\n
36+
Cá trèo lên cây cao, khỉ bơi tung tăng mặt hồ!\n
37+
Thấy em thật là vui khi không buồn và thật buồn khi mà không vui...\n
38+
Anh nói với họ rằng, có kì lân thực sự và con gà nhà anh đã đấm chết một
39+
con voi nặng 69 tấn nhưng họ lại bắt anh, anh tự hỏi vì sao nhỉ em?",
40+
)
41+
.unwrap();
42+
43+
let result = to_utf8(content.as_ptr(), &raw mut out);
44+
let content_str = CStr::from_ptr(out).to_str().unwrap();
45+
46+
assert_ne!(result, R::C_STRING_CONVERT_ERR);
47+
assert_ne!(result, R::NULL_DEFERENCE_ERR);
48+
assert_eq!(result, R::OK);
49+
assert!(!content_str.is_empty());
50+
assert_eq!(
51+
content_str,
52+
"café 世界 🌍\n春の海 ひねもすのたり のたりかな\nشكرا لك.\n
53+
Cá trèo lên cây cao, khỉ bơi tung tăng mặt hồ!\n
54+
Thấy em thật là vui khi không buồn và thật buồn khi mà không vui...\n
55+
Anh nói với họ rằng, có kì lân thực sự và con gà nhà anh đã đấm chết một
56+
con voi nặng 69 tấn nhưng họ lại bắt anh, anh tự hỏi vì sao nhỉ em?"
57+
);
58+
59+
let _ = CString::from_raw(out);
60+
}
61+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef CONTENT_TYPES_HXX
2+
#define CONTENT_TYPES_HXX
3+
4+
#include <cstdint>
5+
using std::uint8_t;
6+
namespace Lazyboard::clipboard {
7+
enum class ContentTypes : uint8_t {
8+
TEXT,
9+
HTML,
10+
URL,
11+
PATH,
12+
IMAGE,
13+
};
14+
}
15+
16+
#endif

src/clipboard/include/text.hxx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef TEXT_CLIPBOARD_HXX
2+
#define TEXT_CLIPBOARD_HXX
3+
#include <qclipboard.h>
4+
5+
#include <memory>
6+
#include <string>
7+
8+
#include "../../front_end_db/include/sqlite_manager.hxx"
9+
10+
using Lazyboard::front_end_db::SQLiteManager;
11+
using std::string;
12+
using std::unique_ptr;
13+
14+
namespace Lazyboard::clipboard {
15+
class TextClipboard {
16+
private:
17+
QClipboard *clipboard = nullptr;
18+
unique_ptr<SQLiteManager> sqlite_manager;
19+
20+
private:
21+
void save_to_cache();
22+
23+
public:
24+
TextClipboard();
25+
void on_changed();
26+
string get();
27+
};
28+
} // namespace Lazyboard::clipboard
29+
30+
#endif // TEXT_CLIPBOARD_HXX

src/clipboard/text.cxx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "include/text.hxx"
2+
3+
#include <qapplication.h>
4+
#include <qclipboard.h>
5+
#include <qobject.h>
6+
7+
#include <memory>
8+
9+
using Lazyboard::clipboard::TextClipboard;
10+
using Self = TextClipboard;
11+
using std::make_unique;
12+
13+
Self::TextClipboard() {
14+
if (!this->clipboard) {
15+
this->clipboard = QApplication::clipboard();
16+
}
17+
18+
sqlite_manager = make_unique<SQLiteManager>();
19+
}
20+
21+
void Self::save_to_cache() {}
22+
23+
void Self::on_changed() {
24+
using O = QObject;
25+
26+
O::connect(this->clipboard, &QClipboard::dataChanged, [] {
27+
28+
});
29+
}
30+
31+
string Self::get() {
32+
auto clipboard_text = this->clipboard->text();
33+
34+
return clipboard_text.toStdString();
35+
}

src/front_end/include/icon_bytes.hxx

Lines changed: 471 additions & 3 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)