Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[dotnet] implement coercive lt, le, gt, ge; add tests to 10-cmp.t

update LHF.txt ;)
  • Loading branch information...
commit 0d13a6cebc2fa21232a74c3029d7d83d90cbea6b 1 parent 0f23784
@diakopter diakopter authored
View
16 common/NQP/NQPSetting.pm
@@ -224,6 +224,22 @@ sub &infix:<ne>($x, $y) {
!nqp::equal_strs($x.Str, $y.Str)
}
+sub &infix:<ge>($x, $y) {
+ nqp::greater_than_or_equal_strs($x.Str, $y.Str)
+}
+
+sub &infix:<gt>($x, $y) {
+ nqp::greater_than_strs($x.Str, $y.Str)
+}
+
+sub &infix:<le>($x, $y) {
+ nqp::less_than_or_equal_strs($x.Str, $y.Str)
+}
+
+sub &infix:<lt>($x, $y) {
+ nqp::less_than_strs($x.Str, $y.Str)
+}
+
sub &infix:<=:=>($x, $y) {
nqp::equal_refs($x, $y)
}
View
12 dotnet/LHF.txt
@@ -8,13 +8,7 @@ on freenode.
String comparative operators
----------------------------
-DIFFICULTY: Easy-Intermediate
-SKILLS: NQP, C#
+DIFFICULTY: Intermediate-Impossible
+SKILLS: Being jnthn
DETAILS:
-At the moment, there is no support for the operators lt, le, gt and
-ge. Two things are needed to make this happen. First, you'll need to
-make some new entries in runtime/Runtime/Ops as a way to actually
-provide the low-level comparative logic. See equal_strings for
-example. Then you'll need to add some entries to NQPSetting.pm (it's
-outside of the dotnet tree, in /common/NQP). Finally, see about
-getting the relevant tests running from the nqp-rx test suite.
+Add task items to this file.
View
56 dotnet/runtime/Runtime/Ops/Comparison.cs
@@ -177,6 +177,62 @@ public static RakudoObject greater_than_or_equal_ints(ThreadContext TC, RakudoOb
return Ops.box_int(TC,
(Ops.unbox_int(TC, x) >= Ops.unbox_int(TC, y) ? 1 : 0),
TC.DefaultBoolBoxType);
+ }
+
+ /// <summary>
+ /// Compares two strings for greater-than inequality.
+ /// </summary>
+ /// <param name="x"></param>
+ /// <param name="y"></param>
+ /// <param name="ResultType"></param>
+ /// <returns></returns>
+ public static RakudoObject greater_than_strs(ThreadContext TC, RakudoObject x, RakudoObject y)
+ {
+ return Ops.box_int(TC,
+ String.Compare(Ops.unbox_str(TC, x), Ops.unbox_str(TC, y)) > 0 ? 1 : 0,
+ TC.DefaultBoolBoxType);
+ }
+
+ /// <summary>
+ /// Compares two strings for greater-than-or-equal inequality.
+ /// </summary>
+ /// <param name="x"></param>
+ /// <param name="y"></param>
+ /// <param name="ResultType"></param>
+ /// <returns></returns>
+ public static RakudoObject greater_than_or_equal_strs(ThreadContext TC, RakudoObject x, RakudoObject y)
+ {
+ return Ops.box_int(TC,
+ String.Compare(Ops.unbox_str(TC, x), Ops.unbox_str(TC, y)) >= 0 ? 1 : 0,
+ TC.DefaultBoolBoxType);
+ }
+
+ /// <summary>
+ /// Compares two strings for less-than inequality.
+ /// </summary>
+ /// <param name="x"></param>
+ /// <param name="y"></param>
+ /// <param name="ResultType"></param>
+ /// <returns></returns>
+ public static RakudoObject less_than_strs(ThreadContext TC, RakudoObject x, RakudoObject y)
+ {
+ return Ops.box_int(TC,
+ String.Compare(Ops.unbox_str(TC, x), Ops.unbox_str(TC, y)) < 0 ? 1 : 0,
+ TC.DefaultBoolBoxType);
+ }
+
+ /// <summary>
+ /// Compares two strings for less-than-or-equal inequality.
+ /// </summary>
+ /// <param name="x"></param>
+ /// <param name="y"></param>
+ /// <param name="ResultType"></param>
+ /// <returns></returns>
+ public static RakudoObject less_than_or_equal_strs(ThreadContext TC, RakudoObject x, RakudoObject y)
+ {
+ return Ops.box_int(TC,
+ String.Compare(Ops.unbox_str(TC, x), Ops.unbox_str(TC, y)) <= 0 ? 1 : 0,
+ TC.DefaultBoolBoxType);
}
}
}
View
32 t/nqp/10-cmp.t
@@ -2,7 +2,7 @@
# check comparisons
-say('1..19');
+say('1..25');
##Integers, positive and negative
@@ -96,3 +96,33 @@ if $x =:= $z {
print("not ");
}
say("ok 19 # container equality, string value");
+
+if "x" gt "y" {
+ print("not ");
+}
+say("ok 20 # string greater-than inequality");
+
+unless "y" gt "x" {
+ print("not ");
+}
+say("ok 21 # string less-than inequality");
+
+unless "y" ge "y" {
+ print("not ");
+}
+say("ok 22 # string greater-than-or-equal inequality");
+
+unless "y" ge "xx" {
+ print("not ");
+}
+say("ok 23 # string greater-than-or-equal inequality");
+
+if "y" le "xx" {
+ print("not ");
+}
+say("ok 24 # string less-than-or-equal inequality");
+
+unless "y" le "y" {
+ print("not ");
+}
+say("ok 25 # string less-than-or-equal inequality");
Please sign in to comment.
Something went wrong with that request. Please try again.