@@ -332,6 +332,67 @@ pub extern "C" fn nstd_core_cstr_get(cstr: &NSTDCStr, pos: NSTDUInt) -> *const N
332
332
core:: ptr:: null ( )
333
333
}
334
334
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
+
335
396
/// A mutable slice of a C string.
336
397
///
337
398
/// # Safety
@@ -754,3 +815,140 @@ pub extern "C" fn nstd_core_cstr_mut_get_const(
754
815
}
755
816
core:: ptr:: null_mut ( )
756
817
}
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