Skip to content

Commit 8e031e2

Browse files
jf2048sdroege
authored andcommitted
glib: use strcmp for GStringPtr comparisons
1 parent 0d38c9a commit 8e031e2

File tree

1 file changed

+74
-21
lines changed

1 file changed

+74
-21
lines changed

glib/src/gstring.rs

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,24 @@ impl GStringPtr {
684684
pub fn to_str(&self) -> &str {
685685
self.to_gstr().as_str()
686686
}
687+
688+
// rustdoc-stripper-ignore-next
689+
/// Returns the string's C pointer.
690+
#[inline]
691+
pub const fn as_ptr(&self) -> *const c_char {
692+
self.0.as_ptr()
693+
}
694+
695+
// rustdoc-stripper-ignore-next
696+
/// Wrapper around `libc::strcmp` returning `Ordering`.
697+
///
698+
/// # Safety
699+
///
700+
/// `a` and `b` must be non-null pointers to nul-terminated C strings.
701+
#[inline]
702+
unsafe fn strcmp(a: *const c_char, b: *const c_char) -> Ordering {
703+
from_glib(libc::strcmp(a, b))
704+
}
687705
}
688706

689707
impl Clone for GStringPtr {
@@ -693,6 +711,13 @@ impl Clone for GStringPtr {
693711
}
694712
}
695713

714+
impl IntoGlibPtr<*mut c_char> for GStringPtr {
715+
#[inline]
716+
unsafe fn into_glib_ptr(self) -> *mut c_char {
717+
self.0.as_ptr()
718+
}
719+
}
720+
696721
impl Drop for GStringPtr {
697722
#[inline]
698723
fn drop(&mut self) {
@@ -704,7 +729,7 @@ impl Drop for GStringPtr {
704729

705730
impl fmt::Debug for GStringPtr {
706731
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
707-
f.write_str(self.to_str())
732+
<&GStr as fmt::Debug>::fmt(&self.to_gstr(), f)
708733
}
709734
}
710735

@@ -720,105 +745,105 @@ impl Eq for GStringPtr {}
720745
impl PartialEq for GStringPtr {
721746
#[inline]
722747
fn eq(&self, other: &GStringPtr) -> bool {
723-
self.to_gstr() == other.to_gstr()
748+
self.partial_cmp(other) == Some(Ordering::Equal)
724749
}
725750
}
726751

727752
impl PartialEq<GStringPtr> for String {
728753
#[inline]
729754
fn eq(&self, other: &GStringPtr) -> bool {
730-
self.as_str() == other.to_str()
755+
self.partial_cmp(other) == Some(Ordering::Equal)
731756
}
732757
}
733758

734759
impl PartialEq<GStringPtr> for GString {
735760
#[inline]
736761
fn eq(&self, other: &GStringPtr) -> bool {
737-
self.as_str() == other.to_str()
762+
self.partial_cmp(other) == Some(Ordering::Equal)
738763
}
739764
}
740765

741766
impl PartialEq<str> for GStringPtr {
742767
#[inline]
743768
fn eq(&self, other: &str) -> bool {
744-
self.to_str() == other
769+
self.partial_cmp(other) == Some(Ordering::Equal)
745770
}
746771
}
747772

748773
impl PartialEq<&str> for GStringPtr {
749774
#[inline]
750775
fn eq(&self, other: &&str) -> bool {
751-
self.to_str() == *other
776+
self.partial_cmp(other) == Some(Ordering::Equal)
752777
}
753778
}
754779

755780
impl PartialEq<GStr> for GStringPtr {
756781
#[inline]
757782
fn eq(&self, other: &GStr) -> bool {
758-
self.to_gstr() == other
783+
self.partial_cmp(other) == Some(Ordering::Equal)
759784
}
760785
}
761786

762787
impl PartialEq<&GStr> for GStringPtr {
763788
#[inline]
764789
fn eq(&self, other: &&GStr) -> bool {
765-
self.to_gstr() == *other
790+
self.partial_cmp(other) == Some(Ordering::Equal)
766791
}
767792
}
768793

769794
impl PartialEq<GStringPtr> for &str {
770795
#[inline]
771796
fn eq(&self, other: &GStringPtr) -> bool {
772-
*self == other.to_str()
797+
self.partial_cmp(other) == Some(Ordering::Equal)
773798
}
774799
}
775800

776801
impl PartialEq<GStringPtr> for &GStr {
777802
#[inline]
778803
fn eq(&self, other: &GStringPtr) -> bool {
779-
self.as_str() == other.to_str()
804+
self.partial_cmp(other) == Some(Ordering::Equal)
780805
}
781806
}
782807

783808
impl PartialEq<String> for GStringPtr {
784809
#[inline]
785810
fn eq(&self, other: &String) -> bool {
786-
self.to_str() == other.as_str()
811+
self.partial_cmp(other) == Some(Ordering::Equal)
787812
}
788813
}
789814

790815
impl PartialEq<GString> for GStringPtr {
791816
#[inline]
792817
fn eq(&self, other: &GString) -> bool {
793-
self.to_str() == other.as_str()
818+
self.partial_cmp(other) == Some(Ordering::Equal)
794819
}
795820
}
796821

797822
impl PartialEq<GStringPtr> for str {
798823
#[inline]
799824
fn eq(&self, other: &GStringPtr) -> bool {
800-
self == other.to_str()
825+
self.partial_cmp(other) == Some(Ordering::Equal)
801826
}
802827
}
803828

804829
impl PartialEq<GStringPtr> for GStr {
805830
#[inline]
806831
fn eq(&self, other: &GStringPtr) -> bool {
807-
self == other.to_gstr()
832+
self.partial_cmp(other) == Some(Ordering::Equal)
808833
}
809834
}
810835

811836
impl PartialOrd<GStringPtr> for GStringPtr {
812837
#[inline]
813838
fn partial_cmp(&self, other: &GStringPtr) -> Option<std::cmp::Ordering> {
814-
Some(self.to_gstr().cmp(other.to_gstr()))
839+
Some(unsafe { GStringPtr::strcmp(self.as_ptr(), other.as_ptr()) })
815840
}
816841
}
817842

818843
impl Ord for GStringPtr {
819844
#[inline]
820845
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
821-
self.to_gstr().cmp(other.to_gstr())
846+
unsafe { GStringPtr::strcmp(self.as_ptr(), other.as_ptr()) }
822847
}
823848
}
824849

@@ -832,21 +857,21 @@ impl PartialOrd<GStringPtr> for String {
832857
impl PartialOrd<GStringPtr> for GString {
833858
#[inline]
834859
fn partial_cmp(&self, other: &GStringPtr) -> Option<std::cmp::Ordering> {
835-
Some(self.as_str().cmp(other.to_str()))
860+
Some(unsafe { GStringPtr::strcmp(self.as_ptr(), other.as_ptr()) })
836861
}
837862
}
838863

839864
impl PartialOrd<String> for GStringPtr {
840865
#[inline]
841866
fn partial_cmp(&self, other: &String) -> Option<std::cmp::Ordering> {
842-
Some(self.to_str().cmp(other.as_str()))
867+
Some(self.to_str().cmp(other))
843868
}
844869
}
845870

846871
impl PartialOrd<GString> for GStringPtr {
847872
#[inline]
848873
fn partial_cmp(&self, other: &GString) -> Option<std::cmp::Ordering> {
849-
Some(self.to_str().cmp(other.as_str()))
874+
Some(unsafe { GStringPtr::strcmp(self.as_ptr(), other.as_ptr()) })
850875
}
851876
}
852877

@@ -860,7 +885,7 @@ impl PartialOrd<GStringPtr> for str {
860885
impl PartialOrd<GStringPtr> for GStr {
861886
#[inline]
862887
fn partial_cmp(&self, other: &GStringPtr) -> Option<std::cmp::Ordering> {
863-
Some(self.cmp(other.to_gstr()))
888+
Some(unsafe { GStringPtr::strcmp(self.as_ptr(), other.as_ptr()) })
864889
}
865890
}
866891

@@ -871,10 +896,38 @@ impl PartialOrd<str> for GStringPtr {
871896
}
872897
}
873898

899+
impl PartialOrd<&str> for GStringPtr {
900+
#[inline]
901+
fn partial_cmp(&self, other: &&str) -> Option<std::cmp::Ordering> {
902+
Some(self.to_str().cmp(other))
903+
}
904+
}
905+
874906
impl PartialOrd<GStr> for GStringPtr {
875907
#[inline]
876908
fn partial_cmp(&self, other: &GStr) -> Option<std::cmp::Ordering> {
877-
Some(self.to_gstr().cmp(other))
909+
Some(unsafe { GStringPtr::strcmp(self.as_ptr(), other.as_ptr()) })
910+
}
911+
}
912+
913+
impl PartialOrd<&GStr> for GStringPtr {
914+
#[inline]
915+
fn partial_cmp(&self, other: &&GStr) -> Option<std::cmp::Ordering> {
916+
Some(unsafe { GStringPtr::strcmp(self.as_ptr(), other.as_ptr()) })
917+
}
918+
}
919+
920+
impl PartialOrd<GStringPtr> for &str {
921+
#[inline]
922+
fn partial_cmp(&self, other: &GStringPtr) -> Option<std::cmp::Ordering> {
923+
Some(self.cmp(&other.to_str()))
924+
}
925+
}
926+
927+
impl PartialOrd<GStringPtr> for &GStr {
928+
#[inline]
929+
fn partial_cmp(&self, other: &GStringPtr) -> Option<std::cmp::Ordering> {
930+
Some(unsafe { GStringPtr::strcmp(self.as_ptr(), other.as_ptr()) })
878931
}
879932
}
880933

0 commit comments

Comments
 (0)