Skip to content

Commit

Permalink
std::strings:: add the Compare function
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Sep 2, 2024
1 parent 47c8408 commit a50176d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions std/strings/compare.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

// Returns an integer comparing two strings lexicographically.
// The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
//
// Use compare when you need to perform a three-way comparison (with
// [slices::SortFunc], for example). It is usually clearer and always faster
// to use the built-in string comparison operators ==, <, >, and so on.
fn Compare(a: str, b: str): int {
mut l := len(a)
if len(b) < l {
l = len(b)
}
mut i := 0
for i < l; i++ {
c1, c2 := a[i], b[i]
if c1 < c2 {
ret -1
}
if c1 > c2 {
ret +1
}
}
if len(a) < len(b) {
ret -1
}
if len(a) > len(b) {
ret +1
}
ret 0
}

0 comments on commit a50176d

Please sign in to comment.