File tree 5 files changed +39
-0
lines changed
5 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ All notable changes to MiniJinja are documented here.
13
13
merge feature, and fixed enumeration behavior when non-map objects
14
14
are attempted to be merged. #745
15
15
- Added ` mj_value_new_bytes ` to the C-ABI. #749
16
+ - Added ` mj_value_as_bytes ` to the C-ABI to borrow from strings or
17
+ byte values. #750
16
18
17
19
## 2.8.0
18
20
Original file line number Diff line number Diff line change 1
1
#include <minijinja.h>
2
2
#include <stdio.h>
3
3
#include <assert.h>
4
+ #include <string.h>
4
5
5
6
int main ()
6
7
{
@@ -60,6 +61,14 @@ seq: {{ seq }}");
60
61
mj_str_free (ervs );
61
62
mj_value_decref (& erv );
62
63
64
+ // you can borrow utf-8 from strings, if they are strings.
65
+ mj_value bv = mj_value_new_string ("Hello" );
66
+ uintptr_t bvlen ;
67
+ const char * bvstr = mj_value_as_bytes (bv , & bvlen );
68
+ assert (bvlen == 5 );
69
+ assert (memcmp (bvstr , "Hello" , 5 ) == 0 );
70
+ mj_value_decref (& bv );
71
+
63
72
mj_env_free (env );
64
73
65
74
return 0 ;
Original file line number Diff line number Diff line change @@ -252,6 +252,11 @@ MINIJINJA_API void mj_syntax_config_default(struct mj_syntax_config *syntax);
252
252
*/
253
253
MINIJINJA_API bool mj_value_append (struct mj_value * slf , struct mj_value value );
254
254
255
+ /*
256
+ If the value is a string or bytes, returns it the pointer
257
+ */
258
+ MINIJINJA_API const char * mj_value_as_bytes (struct mj_value value , uintptr_t * len_out );
259
+
255
260
/*
256
261
Extracts a float from the value
257
262
*/
Original file line number Diff line number Diff line change @@ -21,6 +21,12 @@ impl<T> AbiResult for *mut T {
21
21
}
22
22
}
23
23
24
+ impl < T > AbiResult for * const T {
25
+ fn err_value ( ) -> Self {
26
+ ptr:: null ( )
27
+ }
28
+ }
29
+
24
30
impl AbiResult for u64 {
25
31
fn err_value ( ) -> Self {
26
32
0
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ use std::collections::BTreeMap;
2
2
use std:: ffi:: { c_char, CStr , CString } ;
3
3
use std:: mem:: { transmute, ManuallyDrop } ;
4
4
use std:: ops:: Deref ;
5
+ use std:: ptr;
5
6
use std:: sync:: Arc ;
6
7
7
8
use minijinja:: value:: { Object , ValueIter , ValueKind } ;
@@ -284,6 +285,22 @@ ffi_fn! {
284
285
}
285
286
}
286
287
288
+ ffi_fn ! {
289
+ /// If the value is a string or bytes, returns it the pointer
290
+ /// to it, and the length.
291
+ ///
292
+ /// Note that strings are not null terminated. If you need that, use
293
+ /// `mj_value_to_str` which will also stringify non string values.
294
+ unsafe fn mj_value_as_bytes( _scope, value: mj_value, len_out: & mut usize ) -> * const c_char {
295
+ if let Some ( bytes) = value. as_bytes( ) {
296
+ * len_out = bytes. len( ) ;
297
+ bytes. as_ptr( ) as * const c_char
298
+ } else {
299
+ ptr:: null( )
300
+ }
301
+ }
302
+ }
303
+
287
304
ffi_fn ! {
288
305
/// Extracts an integer from the value
289
306
unsafe fn mj_value_as_i64( _scope, value: mj_value) -> i64 {
You can’t perform that action at this time.
0 commit comments