Skip to content

Commit 262ad7c

Browse files
committed
Use OSC 52 as a fallback for setting the system clipboard
This adds a simple base64 implementation to keep us from adding a crate for one function. It's mostly based on https://github.com/marshallpierce/rust-base64/blob/a675443d327e175f735a37f574de803d6a332591/src/engine/naive.rs#L42
1 parent 9d1793c commit 262ad7c

File tree

7 files changed

+287
-67
lines changed

7 files changed

+287
-67
lines changed

helix-view/src/base64/LICENSE-MIT

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Alice Maz
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

helix-view/src/base64/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
A minimal base64 implementation to keep from pulling in a crate for just that. It's based on
2+
https://github.com/marshallpierce/rust-base64 but without all the customization options.
3+
The biggest portion comes from
4+
https://github.com/marshallpierce/rust-base64/blob/a675443d327e175f735a37f574de803d6a332591/src/engine/naive.rs#L42
5+
Thanks, rust-base64!

helix-view/src/base64/mod.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
use std::ops::{BitAnd, BitOr, Shl, Shr};
2+
3+
const PAD_BYTE: u8 = b'=';
4+
const ENCODE_TABLE: &[u8] =
5+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".as_bytes();
6+
const LOW_SIX_BITS: u32 = 0x3F;
7+
8+
pub fn encode(input: &[u8]) -> String {
9+
let rem = input.len() % 3;
10+
let complete_chunks = input.len() / 3;
11+
let remainder_chunk = if rem == 0 { 0 } else { 1 };
12+
let encoded_size = (complete_chunks + remainder_chunk) * 4;
13+
14+
let mut output = vec![0; encoded_size];
15+
16+
// complete chunks first
17+
let complete_chunk_len = input.len() - rem;
18+
19+
let mut input_index = 0_usize;
20+
let mut output_index = 0_usize;
21+
while input_index < complete_chunk_len {
22+
let chunk = &input[input_index..input_index + 3];
23+
24+
// populate low 24 bits from 3 bytes
25+
let chunk_int: u32 =
26+
(chunk[0] as u32).shl(16) | (chunk[1] as u32).shl(8) | (chunk[2] as u32);
27+
// encode 4x 6-bit output bytes
28+
output[output_index] = ENCODE_TABLE[chunk_int.shr(18) as usize];
29+
output[output_index + 1] = ENCODE_TABLE[chunk_int.shr(12_u8).bitand(LOW_SIX_BITS) as usize];
30+
output[output_index + 2] = ENCODE_TABLE[chunk_int.shr(6_u8).bitand(LOW_SIX_BITS) as usize];
31+
output[output_index + 3] = ENCODE_TABLE[chunk_int.bitand(LOW_SIX_BITS) as usize];
32+
33+
input_index += 3;
34+
output_index += 4;
35+
}
36+
37+
// then leftovers
38+
if rem == 2 {
39+
let chunk = &input[input_index..input_index + 2];
40+
41+
// high six bits of chunk[0]
42+
output[output_index] = ENCODE_TABLE[chunk[0].shr(2) as usize];
43+
// bottom 2 bits of [0], high 4 bits of [1]
44+
output[output_index + 1] = ENCODE_TABLE
45+
[(chunk[0].shl(4_u8).bitor(chunk[1].shr(4_u8)) as u32).bitand(LOW_SIX_BITS) as usize];
46+
// bottom 4 bits of [1], with the 2 bottom bits as zero
47+
output[output_index + 2] =
48+
ENCODE_TABLE[(chunk[1].shl(2_u8) as u32).bitand(LOW_SIX_BITS) as usize];
49+
output[output_index + 3] = PAD_BYTE;
50+
} else if rem == 1 {
51+
let byte = input[input_index];
52+
output[output_index] = ENCODE_TABLE[byte.shr(2) as usize];
53+
output[output_index + 1] =
54+
ENCODE_TABLE[(byte.shl(4_u8) as u32).bitand(LOW_SIX_BITS) as usize];
55+
output[output_index + 2] = PAD_BYTE;
56+
output[output_index + 3] = PAD_BYTE;
57+
}
58+
String::from_utf8(output).expect("Invalid UTF8")
59+
}
60+
61+
#[cfg(test)]
62+
mod tests {
63+
fn compare_encode(expected: &str, target: &[u8]) {
64+
assert_eq!(expected, super::encode(target));
65+
}
66+
67+
#[test]
68+
fn encode_rfc4648_0() {
69+
compare_encode("", b"");
70+
}
71+
72+
#[test]
73+
fn encode_rfc4648_1() {
74+
compare_encode("Zg==", b"f");
75+
}
76+
77+
#[test]
78+
fn encode_rfc4648_2() {
79+
compare_encode("Zm8=", b"fo");
80+
}
81+
82+
#[test]
83+
fn encode_rfc4648_3() {
84+
compare_encode("Zm9v", b"foo");
85+
}
86+
87+
#[test]
88+
fn encode_rfc4648_4() {
89+
compare_encode("Zm9vYg==", b"foob");
90+
}
91+
92+
#[test]
93+
fn encode_rfc4648_5() {
94+
compare_encode("Zm9vYmE=", b"fooba");
95+
}
96+
97+
#[test]
98+
fn encode_rfc4648_6() {
99+
compare_encode("Zm9vYmFy", b"foobar");
100+
}
101+
102+
#[test]
103+
fn encode_all_ascii() {
104+
let mut ascii = Vec::<u8>::with_capacity(128);
105+
106+
for i in 0..128 {
107+
ascii.push(i);
108+
}
109+
110+
compare_encode(
111+
"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7P\
112+
D0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn8\
113+
=",
114+
&ascii,
115+
);
116+
}
117+
118+
#[test]
119+
fn encode_all_bytes() {
120+
let mut bytes = Vec::<u8>::with_capacity(256);
121+
122+
for i in 0..255 {
123+
bytes.push(i);
124+
}
125+
bytes.push(255); //bug with "overflowing" ranges?
126+
127+
compare_encode(
128+
"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7P\
129+
D0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn\
130+
+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6\
131+
/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==",
132+
&bytes,
133+
);
134+
}
135+
}

0 commit comments

Comments
 (0)