MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4// ReSharper disable UnusedMember.Global5// ReSharper disable RedundantCast6 7namespace System.Management.Automation8{9 internal static class Boxed10 {11 internal static readonly object True = (object)true;12 internal static readonly object False = (object)false;13 }14 15 internal static class IntOps16 {17 internal static object Add(int lhs, int rhs)18 {19 long result = (long)lhs + (long)rhs;20 if (result <= int.MaxValue && result >= int.MinValue)21 {22 return (int)result;23 }24 25 return (double)result;26 }27 28 internal static object Sub(int lhs, int rhs)29 {30 long result = (long)lhs - (long)rhs;31 if (result <= int.MaxValue && result >= int.MinValue)32 {33 return (int)result;34 }35 36 return (double)result;37 }38 39 internal static object Multiply(int lhs, int rhs)40 {41 long result = (long)lhs * (long)rhs;42 if (result <= int.MaxValue && result >= int.MinValue)43 {44 return (int)result;45 }46 47 return (double)result;48 }49 50 internal static object Divide(int lhs, int rhs)51 {52 // TBD: is it better to cover the special cases explicitly, or53 // alternatively guard with try/catch?54 55 if (rhs == 0)56 {57 DivideByZeroException dbze = new DivideByZeroException();58 throw new RuntimeException(dbze.Message, dbze);59 }60 61 if (lhs == int.MinValue && rhs == -1)62 {63 // The result of this operation can't fit in an int, so promote.64 return (double)lhs / (double)rhs;65 }66 67 // If the remainder is 0, stay with integer division, otherwise use doubles.68 if ((lhs % rhs) == 0)69 {70 return lhs / rhs;71 }72 73 return (double)lhs / (double)rhs;74 }75 76 internal static object Remainder(int lhs, int rhs)77 {78 // TBD: is it better to cover the special cases explicitly, or79 // alternatively guard with try/catch?80 81 if (rhs == 0)82 {83 DivideByZeroException dbze = new DivideByZeroException();84 throw new RuntimeException(dbze.Message, dbze);85 }86 87 if (lhs == int.MinValue && rhs == -1)88 {89 // The CLR raises an overflow exception for these values. PowerShell typically90 // promotes whenever things overflow, so we just hard code the result value.91 return 0;92 }93 94 return lhs % rhs;95 }96 97 internal static object CompareEq(int lhs, int rhs) { return (lhs == rhs) ? Boxed.True : Boxed.False; }98 99 internal static object CompareNe(int lhs, int rhs) { return (lhs != rhs) ? Boxed.True : Boxed.False; }100 101 internal static object CompareLt(int lhs, int rhs) { return (lhs < rhs) ? Boxed.True : Boxed.False; }102 103 internal static object CompareLe(int lhs, int rhs) { return (lhs <= rhs) ? Boxed.True : Boxed.False; }104 105 internal static object CompareGt(int lhs, int rhs) { return (lhs > rhs) ? Boxed.True : Boxed.False; }106 107 internal static object CompareGe(int lhs, int rhs) { return (lhs >= rhs) ? Boxed.True : Boxed.False; }108 109 internal static object[] Range(int lower, int upper)110 {111 int absRange = Math.Abs(checked(upper - lower));112 113 object[] ra = new object[absRange + 1];114 if (lower > upper)115 {116 // 3 .. 1 => 3 2 1117 for (int offset = 0; offset < ra.Length; offset++)118 ra[offset] = lower--;119 }120 else121 {122 // 1 .. 3 => 1 2 3123 for (int offset = 0; offset < ra.Length; offset++)124 ra[offset] = lower++;125 }126 127 return ra;128 }129 }130 131 internal static class UIntOps132 {133 internal static object Add(uint lhs, uint rhs)134 {135 ulong result = (ulong)lhs + (ulong)rhs;136 if (result <= uint.MaxValue)137 {138 return (uint)result;139 }140 141 return (double)result;142 }143 144 internal static object Sub(uint lhs, uint rhs)145 {146 long result = (long)lhs - (long)rhs;147 if (result >= uint.MinValue)148 {149 return (uint)result;150 }151 152 return (double)result;153 }154 155 internal static object Multiply(uint lhs, uint rhs)156 {157 ulong result = (ulong)lhs * (ulong)rhs;158 if (result <= uint.MaxValue)159 {160 return (uint)result;161 }162 163 return (double)result;164 }165 166 internal static object Divide(uint lhs, uint rhs)167 {168 // TBD: is it better to cover the special cases explicitly, or169 // alternatively guard with try/catch?170 171 if (rhs == 0)172 {173 DivideByZeroException dbze = new DivideByZeroException();174 throw new RuntimeException(dbze.Message, dbze);175 }176 177 // If the remainder is 0, stay with integer division, otherwise use doubles.178 if ((lhs % rhs) == 0)179 {180 return lhs / rhs;181 }182 183 return (double)lhs / (double)rhs;184 }185 186 internal static object Remainder(uint lhs, uint rhs)187 {188 // TBD: is it better to cover the special cases explicitly, or189 // alternatively guard with try/catch?190 191 if (rhs == 0)192 {193 DivideByZeroException dbze = new DivideByZeroException();194 throw new RuntimeException(dbze.Message, dbze);195 }196 197 return lhs % rhs;198 }199 200 internal static object CompareEq(uint lhs, uint rhs) { return (lhs == rhs) ? Boxed.True : Boxed.False; }201 202 internal static object CompareNe(uint lhs, uint rhs) { return (lhs != rhs) ? Boxed.True : Boxed.False; }203 204 internal static object CompareLt(uint lhs, uint rhs) { return (lhs < rhs) ? Boxed.True : Boxed.False; }205 206 internal static object CompareLe(uint lhs, uint rhs) { return (lhs <= rhs) ? Boxed.True : Boxed.False; }207 208 internal static object CompareGt(uint lhs, uint rhs) { return (lhs > rhs) ? Boxed.True : Boxed.False; }209 210 internal static object CompareGe(uint lhs, uint rhs) { return (lhs >= rhs) ? Boxed.True : Boxed.False; }211 }212 213 internal static class LongOps214 {215 internal static object Add(long lhs, long rhs)216 {217 decimal result = (decimal)lhs + (decimal)rhs;218 if (result <= long.MaxValue && result >= long.MinValue)219 {220 return (long)result;221 }222 223 return (double)result;224 }225 226 internal static object Sub(long lhs, long rhs)227 {228 decimal result = (decimal)lhs - (decimal)rhs;229 if (result <= long.MaxValue && result >= long.MinValue)230 {231 return (long)result;232 }233 234 return (double)result;235 }236 237 internal static object Multiply(long lhs, long rhs)238 {239 System.Numerics.BigInteger biLhs = lhs;240 System.Numerics.BigInteger biRhs = rhs;241 System.Numerics.BigInteger biResult = biLhs * biRhs;242 243 if (biResult <= long.MaxValue && biResult >= long.MinValue)244 {245 return (long)biResult;246 }247 248 return (double)biResult;249 }250 251 internal static object Divide(long lhs, long rhs)252 {253 // TBD: is it better to cover the special cases explicitly, or254 // alternatively guard with try/catch?255 256 if (rhs == 0)257 {258 DivideByZeroException dbze = new DivideByZeroException();259 throw new RuntimeException(dbze.Message, dbze);260 }261 262 // Special case.263 // This changes the sign of the min value, causing an integer overflow.264 if (lhs == long.MinValue && rhs == -1)265 {266 return (double)lhs / (double)rhs;267 }268 269 // If the remainder is 0, stay with integer division, otherwise use doubles.270 if ((lhs % rhs) == 0)271 {272 return lhs / rhs;273 }274 275 return (double)lhs / (double)rhs;276 }277 278 internal static object Remainder(long lhs, long rhs)279 {280 // TBD: is it better to cover the special cases explicitly, or281 // alternatively guard with try/catch?282 283 if (rhs == 0)284 {285 DivideByZeroException dbze = new DivideByZeroException();286 throw new RuntimeException(dbze.Message, dbze);287 }288 289 if (lhs == long.MinValue && rhs == -1)290 {291 // The CLR raises an overflow exception for these values. PowerShell typically292 // promotes whenever things overflow, so we just hard code the result value.293 return 0L;294 }295 296 return lhs % rhs;297 }298 299 internal static object CompareEq(long lhs, long rhs) { return (lhs == rhs) ? Boxed.True : Boxed.False; }300 301 internal static object CompareNe(long lhs, long rhs) { return (lhs != rhs) ? Boxed.True : Boxed.False; }302 303 internal static object CompareLt(long lhs, long rhs) { return (lhs < rhs) ? Boxed.True : Boxed.False; }304 305 internal static object CompareLe(long lhs, long rhs) { return (lhs <= rhs) ? Boxed.True : Boxed.False; }306 307 internal static object CompareGt(long lhs, long rhs) { return (lhs > rhs) ? Boxed.True : Boxed.False; }308 309 internal static object CompareGe(long lhs, long rhs) { return (lhs >= rhs) ? Boxed.True : Boxed.False; }310 }311 312 internal static class ULongOps313 {314 internal static object Add(ulong lhs, ulong rhs)315 {316 decimal result = (decimal)lhs + (decimal)rhs;317 if (result <= ulong.MaxValue)318 {319 return (ulong)result;320 }321 322 return (double)result;323 }324 325 internal static object Sub(ulong lhs, ulong rhs)326 {327 decimal result = (decimal)lhs - (decimal)rhs;328 if (result >= ulong.MinValue)329 {330 return (ulong)result;331 }332 333 return (double)result;334 }335 336 internal static object Multiply(ulong lhs, ulong rhs)337 {338 System.Numerics.BigInteger biLhs = lhs;339 System.Numerics.BigInteger biRhs = rhs;340 System.Numerics.BigInteger biResult = biLhs * biRhs;341 342 if (biResult <= ulong.MaxValue)343 {344 return (ulong)biResult;345 }346 347 return (double)biResult;348 }349 350 internal static object Divide(ulong lhs, ulong rhs)351 {352 // TBD: is it better to cover the special cases explicitly, or353 // alternatively guard with try/catch?354 355 if (rhs == 0)356 {357 DivideByZeroException dbze = new DivideByZeroException();358 throw new RuntimeException(dbze.Message, dbze);359 }360 361 // If the remainder is 0, stay with integer division, otherwise use doubles.362 if ((lhs % rhs) == 0)363 {364 return lhs / rhs;365 }366 367 return (double)lhs / (double)rhs;368 }369 370 internal static object Remainder(ulong lhs, ulong rhs)371 {372 // TBD: is it better to cover the special cases explicitly, or373 // alternatively guard with try/catch?374 375 if (rhs == 0)376 {377 DivideByZeroException dbze = new DivideByZeroException();378 throw new RuntimeException(dbze.Message, dbze);379 }380 381 return lhs % rhs;382 }383 384 internal static object CompareEq(ulong lhs, ulong rhs) { return (lhs == rhs) ? Boxed.True : Boxed.False; }385 386 internal static object CompareNe(ulong lhs, ulong rhs) { return (lhs != rhs) ? Boxed.True : Boxed.False; }387 388 internal static object CompareLt(ulong lhs, ulong rhs) { return (lhs < rhs) ? Boxed.True : Boxed.False; }389 390 internal static object CompareLe(ulong lhs, ulong rhs) { return (lhs <= rhs) ? Boxed.True : Boxed.False; }391 392 internal static object CompareGt(ulong lhs, ulong rhs) { return (lhs > rhs) ? Boxed.True : Boxed.False; }393 394 internal static object CompareGe(ulong lhs, ulong rhs) { return (lhs >= rhs) ? Boxed.True : Boxed.False; }395 }396 397 internal static class DecimalOps398 {399 internal static object Add(decimal lhs, decimal rhs)400 {401 try402 {403 return checked(lhs + rhs);404 }405 catch (OverflowException oe)406 {407 throw new RuntimeException(oe.Message, oe);408 }409 }410 411 internal static object Sub(decimal lhs, decimal rhs)412 {413 try414 {415 return checked(lhs - rhs);416 }417 catch (OverflowException oe)418 {419 throw new RuntimeException(oe.Message, oe);420 }421 }422 423 internal static object Multiply(decimal lhs, decimal rhs)424 {425 try426 {427 return checked(lhs * rhs);428 }429 catch (OverflowException oe)430 {431 throw new RuntimeException(oe.Message, oe);432 }433 }434 435 internal static object Divide(decimal lhs, decimal rhs)436 {437 try438 {439 return checked(lhs / rhs);440 }441 catch (OverflowException oe)442 {443 throw new RuntimeException(oe.Message, oe);444 }445 catch (DivideByZeroException dbze)446 {447 throw new RuntimeException(dbze.Message, dbze);448 }449 }450 451 internal static object Remainder(decimal lhs, decimal rhs)452 {453 try454 {455 return checked(lhs % rhs);456 }457 catch (OverflowException oe)458 {459 throw new RuntimeException(oe.Message, oe);460 }461 catch (DivideByZeroException dbze)462 {463 throw new RuntimeException(dbze.Message, dbze);464 }465 }466 467 internal static object BNot(decimal val)468 {469 if (val <= int.MaxValue && val >= int.MinValue)470 {471 return unchecked(~LanguagePrimitives.ConvertTo<int>(val));472 }473 474 if (val <= uint.MaxValue && val >= uint.MinValue)475 {476 return unchecked(~LanguagePrimitives.ConvertTo<uint>(val));477 }478 479 if (val <= long.MaxValue && val >= long.MinValue)480 {481 return unchecked(~LanguagePrimitives.ConvertTo<long>(val));482 }483 484 if (val <= ulong.MaxValue && val >= ulong.MinValue)485 {486 return unchecked(~LanguagePrimitives.ConvertTo<ulong>(val));487 }488 489 LanguagePrimitives.ThrowInvalidCastException(val, typeof(int));490 Diagnostics.Assert(false, "an exception is raised by LanguagePrimitives.ThrowInvalidCastException.");491 return null;492 }493 494 internal static object BOr(decimal lhs, decimal rhs)495 {496 ulong l = ConvertToUlong(lhs);497 ulong r = ConvertToUlong(rhs);498 499 // If either operand is signed, return signed result500 if (lhs < 0 || rhs < 0)501 {502 unchecked503 {504 return (long)(l | r);505 }506 }507 508 return l | r;509 }510 511 internal static object BXor(decimal lhs, decimal rhs)512 {513 ulong l = ConvertToUlong(lhs);514 ulong r = ConvertToUlong(rhs);515 516 // If either operand is signed, return signed result517 if (lhs < 0 || rhs < 0)518 {519 unchecked520 {521 return (long)(l ^ r);522 }523 }524 525 return l ^ r;526 }527 528 internal static object BAnd(decimal lhs, decimal rhs)529 {530 ulong l = ConvertToUlong(lhs);531 ulong r = ConvertToUlong(rhs);532 533 // If either operand is signed, return signed result534 if (lhs < 0 || rhs < 0)535 {536 unchecked537 {538 return (long)(l & r);539 }540 }541 542 return l & r;543 }544 545 // This had to be done because if we try to cast a negative decimal number to unsigned, we get an OverFlowException546 // We had to cast them to long (if they are negative) and then promote everything to ULong.547 // While returning the result, we can return either signed or unsigned depending on the input.548 private static ulong ConvertToUlong(decimal val)549 {550 if (val < 0)551 {552 long lValue = LanguagePrimitives.ConvertTo<long>(val);553 return unchecked((ulong)lValue);554 }555 556 return LanguagePrimitives.ConvertTo<ulong>(val);557 }558 559 internal static object LeftShift(decimal val, int count)560 {561 if (val <= int.MaxValue && val >= int.MinValue)562 {563 return unchecked(LanguagePrimitives.ConvertTo<int>(val) << count);564 }565 566 if (val <= uint.MaxValue && val >= uint.MinValue)567 {568 return unchecked(LanguagePrimitives.ConvertTo<uint>(val) << count);569 }570 571 if (val <= long.MaxValue && val >= long.MinValue)572 {573 return unchecked(LanguagePrimitives.ConvertTo<long>(val) << count);574 }575 576 if (val <= ulong.MaxValue && val >= ulong.MinValue)577 {578 return unchecked(LanguagePrimitives.ConvertTo<ulong>(val) << count);579 }580 581 LanguagePrimitives.ThrowInvalidCastException(val, typeof(int));582 Diagnostics.Assert(false, "an exception is raised by LanguagePrimitives.ThrowInvalidCastException.");583 return null;584 }585 586 internal static object RightShift(decimal val, int count)587 {588 if (val <= int.MaxValue && val >= int.MinValue)589 {590 return unchecked(LanguagePrimitives.ConvertTo<int>(val) >> count);591 }592 593 if (val <= uint.MaxValue && val >= uint.MinValue)594 {595 return unchecked(LanguagePrimitives.ConvertTo<uint>(val) >> count);596 }597 598 if (val <= long.MaxValue && val >= long.MinValue)599 {600 return unchecked(LanguagePrimitives.ConvertTo<long>(val) >> count);601 }602 603 if (val <= ulong.MaxValue && val >= ulong.MinValue)604 {605 return unchecked(LanguagePrimitives.ConvertTo<ulong>(val) >> count);606 }607 608 LanguagePrimitives.ThrowInvalidCastException(val, typeof(int));609 Diagnostics.Assert(false, "an exception is raised by LanguagePrimitives.ThrowInvalidCastException.");610 return null;611 }612 613 internal static object CompareEq(decimal lhs, decimal rhs) { return (lhs == rhs) ? Boxed.True : Boxed.False; }614 615 internal static object CompareNe(decimal lhs, decimal rhs) { return (lhs != rhs) ? Boxed.True : Boxed.False; }616 617 internal static object CompareLt(decimal lhs, decimal rhs) { return (lhs < rhs) ? Boxed.True : Boxed.False; }618 619 internal static object CompareLe(decimal lhs, decimal rhs) { return (lhs <= rhs) ? Boxed.True : Boxed.False; }620 621 internal static object CompareGt(decimal lhs, decimal rhs) { return (lhs > rhs) ? Boxed.True : Boxed.False; }622 623 internal static object CompareGe(decimal lhs, decimal rhs) { return (lhs >= rhs) ? Boxed.True : Boxed.False; }624 625 private static object CompareWithDouble(decimal left, double right,626 Func<double, double, object> doubleComparer,627 Func<decimal, decimal, object> decimalComparer)628 {629 decimal rightAsDecimal;630 try631 {632 rightAsDecimal = (decimal)right;633 }634 catch (OverflowException)635 {636 return doubleComparer((double)left, right);637 }638 639 return decimalComparer(left, rightAsDecimal);640 }641 642 private static object CompareWithDouble(double left, decimal right,643 Func<double, double, object> doubleComparer,644 Func<decimal, decimal, object> decimalComparer)645 {646 decimal leftAsDecimal;647 try648 {649 leftAsDecimal = (decimal)left;650 }651 catch (OverflowException)652 {653 return doubleComparer(left, (double)right);654 }655 656 return decimalComparer(leftAsDecimal, right);657 }658 659 internal static object CompareEq1(double lhs, decimal rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareEq, CompareEq); }660 661 internal static object CompareNe1(double lhs, decimal rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareNe, CompareNe); }662 663 internal static object CompareLt1(double lhs, decimal rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareLt, CompareLt); }664 665 internal static object CompareLe1(double lhs, decimal rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareLe, CompareLe); }666 667 internal static object CompareGt1(double lhs, decimal rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareGt, CompareGt); }668 669 internal static object CompareGe1(double lhs, decimal rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareGe, CompareGe); }670 671 internal static object CompareEq2(decimal lhs, double rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareEq, CompareEq); }672 673 internal static object CompareNe2(decimal lhs, double rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareNe, CompareNe); }674 675 internal static object CompareLt2(decimal lhs, double rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareLt, CompareLt); }676 677 internal static object CompareLe2(decimal lhs, double rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareLe, CompareLe); }678 679 internal static object CompareGt2(decimal lhs, double rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareGt, CompareGt); }680 681 internal static object CompareGe2(decimal lhs, double rhs) { return CompareWithDouble(lhs, rhs, DoubleOps.CompareGe, CompareGe); }682 }683 684 internal static class DoubleOps685 {686 internal static object Add(double lhs, double rhs)687 {688 return lhs + rhs;689 }690 691 internal static object Sub(double lhs, double rhs)692 {693 return lhs - rhs;694 }695 696 internal static object Multiply(double lhs, double rhs)697 {698 return lhs * rhs;699 }700 701 internal static object Divide(double lhs, double rhs)702 {703 return lhs / rhs;704 }705 706 internal static object Remainder(double lhs, double rhs)707 {708 return lhs % rhs;709 }710 711 internal static object BNot(double val)712 {713 try714 {715 checked716 {717 if (val <= int.MaxValue && val >= int.MinValue)718 {719 return ~LanguagePrimitives.ConvertTo<int>(val);720 }721 722 if (val <= uint.MaxValue && val >= uint.MinValue)723 {724 return ~LanguagePrimitives.ConvertTo<uint>(val);725 }726 727 if (val <= long.MaxValue && val >= long.MinValue)728 {729 return ~LanguagePrimitives.ConvertTo<long>(val);730 }731 732 if (val <= ulong.MaxValue && val >= ulong.MinValue)733 {734 return ~LanguagePrimitives.ConvertTo<ulong>(val);735 }736 }737 }738 catch (OverflowException)739 {740 }741 742 LanguagePrimitives.ThrowInvalidCastException(val, typeof(ulong));743 Diagnostics.Assert(false, "an exception is raised by LanguagePrimitives.ThrowInvalidCastException.");744 return null;745 }746 747 internal static object BOr(double lhs, double rhs)748 {749 ulong l = ConvertToUlong(lhs);750 ulong r = ConvertToUlong(rhs);751 752 // If either operand is signed, return signed result753 if (lhs < 0 || rhs < 0)754 {755 unchecked756 {757 return (long)(l | r);758 }759 }760 761 return l | r;762 }763 764 internal static object BXor(double lhs, double rhs)765 {766 ulong l = ConvertToUlong(lhs);767 ulong r = ConvertToUlong(rhs);768 769 // If either operand is signed, return signed result770 if (lhs < 0 || rhs < 0)771 {772 unchecked773 {774 return (long)(l ^ r);775 }776 }777 778 return l ^ r;779 }780 781 internal static object BAnd(double lhs, double rhs)782 {783 ulong l = ConvertToUlong(lhs);784 ulong r = ConvertToUlong(rhs);785 786 // If either operand is signed, return signed result787 if (lhs < 0 || rhs < 0)788 {789 unchecked790 {791 return (long)(l & r);792 }793 }794 795 return l & r;796 }797 798 // This had to be done because if we try to cast a negative double number to unsigned, we get an OverFlowException799 // We had to cast them to long (if they are negative) and then promote everything to ULong.800 // While returning the result, we can return either signed or unsigned depending on the input.801 private static ulong ConvertToUlong(double val)802 {803 if (val < 0)804 {805 long lValue = LanguagePrimitives.ConvertTo<long>(val);806 return unchecked((ulong)lValue);807 }808 809 return LanguagePrimitives.ConvertTo<ulong>(val);810 }811 812 internal static object LeftShift(double val, int count)813 {814 checked815 {816 if (val <= int.MaxValue && val >= int.MinValue)817 {818 return LanguagePrimitives.ConvertTo<int>(val) << count;819 }820 821 if (val <= uint.MaxValue && val >= uint.MinValue)822 {823 return LanguagePrimitives.ConvertTo<uint>(val) << count;824 }825 826 if (val <= long.MaxValue && val >= long.MinValue)827 {828 return LanguagePrimitives.ConvertTo<long>(val) << count;829 }830 831 if (val <= ulong.MaxValue && val >= ulong.MinValue)832 {833 return LanguagePrimitives.ConvertTo<ulong>(val) << count;834 }835 }836 837 LanguagePrimitives.ThrowInvalidCastException(val, typeof(ulong));838 Diagnostics.Assert(false, "an exception is raised by LanguagePrimitives.ThrowInvalidCastException.");839 return null;840 }841 842 internal static object RightShift(double val, int count)843 {844 checked845 {846 if (val <= int.MaxValue && val >= int.MinValue)847 {848 return LanguagePrimitives.ConvertTo<int>(val) >> count;849 }850 851 if (val <= uint.MaxValue && val >= uint.MinValue)852 {853 return LanguagePrimitives.ConvertTo<uint>(val) >> count;854 }855 856 if (val <= long.MaxValue && val >= long.MinValue)857 {858 return LanguagePrimitives.ConvertTo<long>(val) >> count;859 }860 861 if (val <= ulong.MaxValue && val >= ulong.MinValue)862 {863 return LanguagePrimitives.ConvertTo<ulong>(val) >> count;864 }865 }866 867 LanguagePrimitives.ThrowInvalidCastException(val, typeof(ulong));868 Diagnostics.Assert(false, "an exception is raised by LanguagePrimitives.ThrowInvalidCastException.");869 return null;870 }871 872 internal static object CompareEq(double lhs, double rhs) { return (lhs == rhs) ? Boxed.True : Boxed.False; }873 874 internal static object CompareNe(double lhs, double rhs) { return (lhs != rhs) ? Boxed.True : Boxed.False; }875 876 internal static object CompareLt(double lhs, double rhs) { return (lhs < rhs) ? Boxed.True : Boxed.False; }877 878 internal static object CompareLe(double lhs, double rhs) { return (lhs <= rhs) ? Boxed.True : Boxed.False; }879 880 internal static object CompareGt(double lhs, double rhs) { return (lhs > rhs) ? Boxed.True : Boxed.False; }881 882 internal static object CompareGe(double lhs, double rhs) { return (lhs >= rhs) ? Boxed.True : Boxed.False; }883 }884 885 internal static class CharOps886 {887 internal static object CompareStringIeq(char lhs, string rhs)888 {889 if (rhs.Length != 1)890 {891 return Boxed.False;892 }893 894 return CompareIeq(lhs, rhs[0]);895 }896 897 internal static object CompareStringIne(char lhs, string rhs)898 {899 if (rhs.Length != 1)900 {901 return Boxed.True;902 }903 904 return CompareIne(lhs, rhs[0]);905 }906 907 internal static object CompareIeq(char lhs, char rhs)908 {909 char firstAsUpper = char.ToUpperInvariant(lhs);910 char secondAsUpper = char.ToUpperInvariant(rhs);911 return firstAsUpper == secondAsUpper ? Boxed.True : Boxed.False;912 }913 914 internal static object CompareIne(char lhs, char rhs)915 {916 char firstAsUpper = char.ToUpperInvariant(lhs);917 char secondAsUpper = char.ToUpperInvariant(rhs);918 return firstAsUpper != secondAsUpper ? Boxed.True : Boxed.False;919 }920 921 internal static object[] Range(char start, char end)922 {923 int lower = (int)start;924 int upper = (int)end;925 926 int absRange = Math.Abs(checked(upper - lower));927 928 object[] ra = new object[absRange + 1];929 if (lower > upper)930 {931 // 3 .. 1 => 3 2 1932 for (int offset = 0; offset < ra.Length; offset++)933 ra[offset] = (char)lower--;934 }935 else936 {937 // 1 .. 3 => 1 2 3938 for (int offset = 0; offset < ra.Length; offset++)939 ra[offset] = (char)lower++;940 }941 942 return ra;943 }944 }945}946 