mirror of
https://github.com/typst/typst
synced 2025-05-18 11:05:28 +08:00
Make bytes joinable
This commit is contained in:
parent
99ddbafc09
commit
d52493938e
@ -446,7 +446,7 @@ impl Add for Array {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AddAssign for Array {
|
impl AddAssign for Array {
|
||||||
fn add_assign(&mut self, rhs: Array) {
|
fn add_assign(&mut self, rhs: Self) {
|
||||||
self.0.extend(rhs.0);
|
self.0.extend(rhs.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::fmt::{self, Debug, Formatter};
|
use std::fmt::{self, Debug, Formatter};
|
||||||
use std::ops::Deref;
|
use std::ops::{Add, AddAssign, Deref};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use comemo::Prehashed;
|
use comemo::Prehashed;
|
||||||
@ -96,6 +96,31 @@ impl Debug for Bytes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Add for Bytes {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(mut self, rhs: Self) -> Self::Output {
|
||||||
|
self += rhs;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AddAssign for Bytes {
|
||||||
|
fn add_assign(&mut self, rhs: Self) {
|
||||||
|
if rhs.is_empty() {
|
||||||
|
// Nothing to do
|
||||||
|
} else if self.is_empty() {
|
||||||
|
*self = rhs;
|
||||||
|
} else if Arc::strong_count(&self.0) == 1 && matches!(**self.0, Cow::Owned(_)) {
|
||||||
|
Arc::make_mut(&mut self.0).update(|cow| {
|
||||||
|
cow.to_mut().extend_from_slice(&rhs);
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
*self = Self::from([self.as_slice(), rhs.as_slice()].concat());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Serialize for Bytes {
|
impl Serialize for Bytes {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
|
@ -26,6 +26,7 @@ pub fn join(lhs: Value, rhs: Value) -> StrResult<Value> {
|
|||||||
(Str(a), Str(b)) => Str(a + b),
|
(Str(a), Str(b)) => Str(a + b),
|
||||||
(Str(a), Symbol(b)) => Str(format_str!("{a}{b}")),
|
(Str(a), Symbol(b)) => Str(format_str!("{a}{b}")),
|
||||||
(Symbol(a), Str(b)) => Str(format_str!("{a}{b}")),
|
(Symbol(a), Str(b)) => Str(format_str!("{a}{b}")),
|
||||||
|
(Bytes(a), Bytes(b)) => Bytes(a + b),
|
||||||
(Content(a), Content(b)) => Content(a + b),
|
(Content(a), Content(b)) => Content(a + b),
|
||||||
(Content(a), Symbol(b)) => Content(a + item!(text)(b.get().into())),
|
(Content(a), Symbol(b)) => Content(a + item!(text)(b.get().into())),
|
||||||
(Content(a), Str(b)) => Content(a + item!(text)(b.into())),
|
(Content(a), Str(b)) => Content(a + item!(text)(b.into())),
|
||||||
@ -96,6 +97,7 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> {
|
|||||||
(Str(a), Str(b)) => Str(a + b),
|
(Str(a), Str(b)) => Str(a + b),
|
||||||
(Str(a), Symbol(b)) => Str(format_str!("{a}{b}")),
|
(Str(a), Symbol(b)) => Str(format_str!("{a}{b}")),
|
||||||
(Symbol(a), Str(b)) => Str(format_str!("{a}{b}")),
|
(Symbol(a), Str(b)) => Str(format_str!("{a}{b}")),
|
||||||
|
(Bytes(a), Bytes(b)) => Bytes(a + b),
|
||||||
(Content(a), Content(b)) => Content(a + b),
|
(Content(a), Content(b)) => Content(a + b),
|
||||||
(Content(a), Symbol(b)) => Content(a + item!(text)(b.get().into())),
|
(Content(a), Symbol(b)) => Content(a + item!(text)(b.get().into())),
|
||||||
(Content(a), Str(b)) => Content(a + item!(text)(b.into())),
|
(Content(a), Str(b)) => Content(a + item!(text)(b.into())),
|
||||||
|
@ -12,6 +12,17 @@
|
|||||||
#test(str(bytes(range(0x41, 0x50))), "ABCDEFGHIJKLMNO")
|
#test(str(bytes(range(0x41, 0x50))), "ABCDEFGHIJKLMNO")
|
||||||
#test(array(bytes("Hello")), (0x48, 0x65, 0x6C, 0x6C, 0x6F))
|
#test(array(bytes("Hello")), (0x48, 0x65, 0x6C, 0x6C, 0x6F))
|
||||||
|
|
||||||
|
---
|
||||||
|
// Test addition and joining.
|
||||||
|
#test(bytes((1, 2)) + bytes(()), bytes((1, 2)))
|
||||||
|
#test(bytes((1, 2)) + bytes((3, 4)), bytes((1, 2, 3, 4)))
|
||||||
|
#test(bytes(()) + bytes((3, 4)), bytes((3, 4)))
|
||||||
|
#test(str({
|
||||||
|
bytes("Hello")
|
||||||
|
bytes((0x20,))
|
||||||
|
bytes("World")
|
||||||
|
}), "Hello World")
|
||||||
|
|
||||||
---
|
---
|
||||||
// Error: 8-14 expected string, array, or bytes, found dictionary
|
// Error: 8-14 expected string, array, or bytes, found dictionary
|
||||||
#bytes((a: 1))
|
#bytes((a: 1))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user