-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
std::strings:: add the Compare function
- Loading branch information
1 parent
47c8408
commit a50176d
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |