Skip to content

Commit 6c651d3

Browse files
Added nstd_core_cstr[_mut]_[first|last][_const].
1 parent e9e3484 commit 6c651d3

File tree

3 files changed

+265
-0
lines changed

3 files changed

+265
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- The overflow behavior for the "release" profile has been set to panic.
44
- The panic behavior for the "release" profile has been set to abort.
55
### `nstd.core`
6+
- Added `cstr[_mut]_[first|last][_const]`.
67
- Added `ops`.
78
- Added `cty_is_unicode`.
89
- Renamed `str[_mut]_get_char` to `str[_mut]_get`.

include/nstd/core/cstr/cstr.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,28 @@ NSTDAPI const NSTDChar *nstd_core_cstr_get_null(const NSTDCStr *cstr);
153153
/// string slice's boundaries.
154154
NSTDAPI const NSTDChar *nstd_core_cstr_get(const NSTDCStr *cstr, NSTDUInt pos);
155155

156+
/// Returns a pointer to the first character in a C string slice, or null if it is empty.
157+
///
158+
/// # Parameters:
159+
///
160+
/// - `const NSTDCStr *cstr` - The C string slice.
161+
///
162+
/// # Returns
163+
///
164+
/// `const NSTDChar *first` - If present, a pointer to the first character in the C string slice.
165+
NSTDAPI const NSTDChar *nstd_core_cstr_first(const NSTDCStr *cstr);
166+
167+
/// Returns a pointer to the last character in a C string slice, or null if it is empty.
168+
///
169+
/// # Parameters:
170+
///
171+
/// - `const NSTDCStr *cstr` - The C string slice.
172+
///
173+
/// # Returns
174+
///
175+
/// `const NSTDChar *last` - If present, a pointer to the last character in the C string slice.
176+
NSTDAPI const NSTDChar *nstd_core_cstr_last(const NSTDCStr *cstr);
177+
156178
/// A mutable slice of a C string.
157179
///
158180
/// # Safety
@@ -366,4 +388,48 @@ NSTDAPI NSTDChar *nstd_core_cstr_mut_get(NSTDCStrMut *cstr, NSTDUInt pos);
366388
/// string slice's boundaries.
367389
NSTDAPI const NSTDChar *nstd_core_cstr_mut_get_const(const NSTDCStrMut *cstr, NSTDUInt pos);
368390

391+
/// Returns a pointer to the first character in a C string slice, or null if it is empty.
392+
///
393+
/// # Parameters:
394+
///
395+
/// - `NSTDCStrMut *cstr` - The C string slice.
396+
///
397+
/// # Returns
398+
///
399+
/// `NSTDChar *first` - If present, a pointer to the first character in the C string slice.
400+
NSTDAPI NSTDChar *nstd_core_cstr_mut_first(NSTDCStrMut *cstr);
401+
402+
/// Returns an immutable pointer to the first character in a C string slice, or null if it is empty.
403+
///
404+
/// # Parameters:
405+
///
406+
/// - `const NSTDCStrMut *cstr` - The C string slice.
407+
///
408+
/// # Returns
409+
///
410+
/// `const NSTDChar *first` - If present, a pointer to the first character in the C string slice.
411+
NSTDAPI const NSTDChar *nstd_core_cstr_mut_first_const(const NSTDCStrMut *cstr);
412+
413+
/// Returns a pointer to the last character in a C string slice, or null if it is empty.
414+
///
415+
/// # Parameters:
416+
///
417+
/// - `NSTDCStrMut *cstr` - The C string slice.
418+
///
419+
/// # Returns
420+
///
421+
/// `NSTDChar *last` - If present, a pointer to the last character in the C string slice.
422+
NSTDAPI NSTDChar *nstd_core_cstr_mut_last(NSTDCStrMut *cstr);
423+
424+
/// Returns an immutable pointer to the last character in a C string slice, or null if it is empty.
425+
///
426+
/// # Parameters:
427+
///
428+
/// - `const NSTDCStrMut *cstr` - The C string slice.
429+
///
430+
/// # Returns
431+
///
432+
/// `const NSTDChar *last` - If present, a pointer to the last character in the C string slice.
433+
NSTDAPI const NSTDChar *nstd_core_cstr_mut_last_const(const NSTDCStrMut *cstr);
434+
369435
#endif

src/core/cstr.rs

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,67 @@ pub extern "C" fn nstd_core_cstr_get(cstr: &NSTDCStr, pos: NSTDUInt) -> *const N
332332
core::ptr::null()
333333
}
334334

335+
/// Returns a pointer to the first character in a C string slice, or null if it is empty.
336+
///
337+
/// # Parameters:
338+
///
339+
/// - `const NSTDCStr *cstr` - The C string slice.
340+
///
341+
/// # Returns
342+
///
343+
/// `const NSTDChar *first` - If present, a pointer to the first character in the C string slice.
344+
///
345+
/// # Example
346+
///
347+
/// ```
348+
/// use nstd_sys::{
349+
/// core::cstr::{nstd_core_cstr_first, nstd_core_cstr_from_raw},
350+
/// NSTDChar,
351+
/// };
352+
///
353+
/// unsafe {
354+
/// let cstr = nstd_core_cstr_from_raw("Tea\0".as_ptr().cast());
355+
/// assert!(*nstd_core_cstr_first(&cstr) == b'T' as NSTDChar);
356+
/// }
357+
/// ```
358+
#[inline]
359+
#[cfg_attr(feature = "clib", no_mangle)]
360+
pub extern "C" fn nstd_core_cstr_first(cstr: &NSTDCStr) -> *const NSTDChar {
361+
match cstr.len > 0 {
362+
true => cstr.ptr,
363+
false => core::ptr::null(),
364+
}
365+
}
366+
367+
/// Returns a pointer to the last character in a C string slice, or null if it is empty.
368+
///
369+
/// # Parameters:
370+
///
371+
/// - `const NSTDCStr *cstr` - The C string slice.
372+
///
373+
/// # Returns
374+
///
375+
/// `const NSTDChar *last` - If present, a pointer to the last character in the C string slice.
376+
///
377+
/// # Example
378+
///
379+
/// ```
380+
/// use nstd_sys::core::cstr::{nstd_core_cstr_from_raw_with_null, nstd_core_cstr_last};
381+
///
382+
/// unsafe {
383+
/// let cstr = nstd_core_cstr_from_raw_with_null("Tea\0".as_ptr().cast());
384+
/// assert!(*nstd_core_cstr_last(&cstr) == 0);
385+
/// }
386+
/// ```
387+
#[inline]
388+
#[cfg_attr(feature = "clib", no_mangle)]
389+
pub extern "C" fn nstd_core_cstr_last(cstr: &NSTDCStr) -> *const NSTDChar {
390+
match cstr.len > 0 {
391+
true => nstd_core_cstr_get(cstr, cstr.len - 1),
392+
false => core::ptr::null(),
393+
}
394+
}
395+
335396
/// A mutable slice of a C string.
336397
///
337398
/// # Safety
@@ -754,3 +815,140 @@ pub extern "C" fn nstd_core_cstr_mut_get_const(
754815
}
755816
core::ptr::null_mut()
756817
}
818+
819+
/// Returns a pointer to the first character in a C string slice, or null if it is empty.
820+
///
821+
/// # Parameters:
822+
///
823+
/// - `NSTDCStrMut *cstr` - The C string slice.
824+
///
825+
/// # Returns
826+
///
827+
/// `NSTDChar *first` - If present, a pointer to the first character in the C string slice.
828+
///
829+
/// # Example
830+
///
831+
/// ```
832+
/// use nstd_sys::{
833+
/// core::cstr::{nstd_core_cstr_mut_first, nstd_core_cstr_mut_from_raw},
834+
/// NSTDChar,
835+
/// };
836+
///
837+
/// let mut s_str = String::from("Bea\0");
838+
///
839+
/// unsafe {
840+
/// let mut cstr = nstd_core_cstr_mut_from_raw(s_str.as_mut_ptr().cast());
841+
/// *nstd_core_cstr_mut_first(&mut cstr) = b'T' as NSTDChar;
842+
/// assert!(s_str == "Tea\0");
843+
/// }
844+
/// ```
845+
#[inline]
846+
#[cfg_attr(feature = "clib", no_mangle)]
847+
pub extern "C" fn nstd_core_cstr_mut_first(cstr: &mut NSTDCStrMut) -> *mut NSTDChar {
848+
match cstr.len > 0 {
849+
true => cstr.ptr,
850+
false => core::ptr::null_mut(),
851+
}
852+
}
853+
854+
/// Returns an immutable pointer to the first character in a C string slice, or null if it is empty.
855+
///
856+
/// # Parameters:
857+
///
858+
/// - `const NSTDCStrMut *cstr` - The C string slice.
859+
///
860+
/// # Returns
861+
///
862+
/// `const NSTDChar *first` - If present, a pointer to the first character in the C string slice.
863+
///
864+
/// # Example
865+
///
866+
/// ```
867+
/// use nstd_sys::{
868+
/// core::cstr::{nstd_core_cstr_mut_first_const, nstd_core_cstr_mut_from_raw},
869+
/// NSTDChar,
870+
/// };
871+
///
872+
/// let mut s_str = String::from("Tea\0");
873+
///
874+
/// unsafe {
875+
/// let cstr = nstd_core_cstr_mut_from_raw(s_str.as_mut_ptr().cast());
876+
/// assert!(*nstd_core_cstr_mut_first_const(&cstr) == b'T' as NSTDChar);
877+
/// }
878+
/// ```
879+
#[inline]
880+
#[cfg_attr(feature = "clib", no_mangle)]
881+
pub extern "C" fn nstd_core_cstr_mut_first_const(cstr: &NSTDCStrMut) -> *const NSTDChar {
882+
match cstr.len > 0 {
883+
true => cstr.ptr,
884+
false => core::ptr::null(),
885+
}
886+
}
887+
888+
/// Returns a pointer to the last character in a C string slice, or null if it is empty.
889+
///
890+
/// # Parameters:
891+
///
892+
/// - `NSTDCStrMut *cstr` - The C string slice.
893+
///
894+
/// # Returns
895+
///
896+
/// `NSTDChar *last` - If present, a pointer to the last character in the C string slice.
897+
///
898+
/// # Example
899+
///
900+
/// ```
901+
/// use nstd_sys::{
902+
/// core::cstr::{nstd_core_cstr_mut_from_raw, nstd_core_cstr_mut_last},
903+
/// NSTDChar,
904+
/// };
905+
///
906+
/// let mut s_str = String::from("Ted\0");
907+
///
908+
/// unsafe {
909+
/// let mut cstr = nstd_core_cstr_mut_from_raw(s_str.as_mut_ptr().cast());
910+
/// *nstd_core_cstr_mut_last(&mut cstr) = b'a' as NSTDChar;
911+
/// assert!(s_str == "Tea\0");
912+
/// }
913+
/// ```
914+
#[inline]
915+
#[cfg_attr(feature = "clib", no_mangle)]
916+
pub extern "C" fn nstd_core_cstr_mut_last(cstr: &mut NSTDCStrMut) -> *mut NSTDChar {
917+
match cstr.len > 0 {
918+
true => nstd_core_cstr_mut_get(cstr, cstr.len - 1),
919+
false => core::ptr::null_mut(),
920+
}
921+
}
922+
923+
/// Returns an immutable pointer to the last character in a C string slice, or null if it is empty.
924+
///
925+
/// # Parameters:
926+
///
927+
/// - `const NSTDCStrMut *cstr` - The C string slice.
928+
///
929+
/// # Returns
930+
///
931+
/// `const NSTDChar *last` - If present, a pointer to the last character in the C string slice.
932+
///
933+
/// # Example
934+
///
935+
/// ```
936+
/// use nstd_sys::core::cstr::{
937+
/// nstd_core_cstr_mut_from_raw_with_null, nstd_core_cstr_mut_last_const,
938+
/// };
939+
///
940+
/// let mut s_str = String::from("Tea\0");
941+
///
942+
/// unsafe {
943+
/// let cstr = nstd_core_cstr_mut_from_raw_with_null(s_str.as_mut_ptr().cast());
944+
/// assert!(*nstd_core_cstr_mut_last_const(&cstr) == 0);
945+
/// }
946+
/// ```
947+
#[inline]
948+
#[cfg_attr(feature = "clib", no_mangle)]
949+
pub extern "C" fn nstd_core_cstr_mut_last_const(cstr: &NSTDCStrMut) -> *const NSTDChar {
950+
match cstr.len > 0 {
951+
true => nstd_core_cstr_mut_get_const(cstr, cstr.len - 1),
952+
false => core::ptr::null(),
953+
}
954+
}

0 commit comments

Comments
 (0)