1. class Collation {
  2. has int $.collation-level = 85;
  3. has $!Country = 'International';
  4. method gist {
  5. "collation-level => $!collation-level, Country => $!Country, " ~
  6. "Language => None, primary => {self.primary}, secondary => {self.secondary}, " ~
  7. "tertiary => {self.tertiary}, quaternary => {self.quaternary}"
  8. }
  9. method set (
  10. Int :$primary = 1,
  11. Int :$secondary = 1,
  12. Int :$tertiary = 1,
  13. Int :$quaternary = 1)
  14. {
  15. nqp::isnull(nqp::getlexcaller('EXPERIMENTAL-COLLATION')) and X::Experimental.new(
  16. feature => 'the $*COLLATION dynamic variable',
  17. use => 'collation'
  18. ).throw;
  19. my int $i = 0;
  20. $i += 1 if $primary.sign == 1;
  21. $i += 2 if $primary.sign == -1;
  22. $i += 4 if $secondary.sign == 1;
  23. $i += 8 if $secondary.sign == -1;
  24. $i += 16 if $tertiary.sign == 1;
  25. $i += 32 if $tertiary.sign == -1;
  26. $i += 64 if $quaternary.sign == 1;
  27. $i += 128 if $quaternary.sign == -1;
  28. $!collation-level = $i;
  29. self;
  30. }
  31. method check ($more, $less) {
  32. # Hopefully the user didn't set collation-level manually to have a level
  33. # both enabled *and* disabled. But check if this is the case anyway.
  34. return 0 if $!collation-level +& all($more,$less);
  35. return 1 if $!collation-level +& $more;
  36. return -1 if $!collation-level +& $less;
  37. return 0;
  38. }
  39. method primary { self.check( 1, 2) }
  40. method secondary { self.check( 4, 8) }
  41. method tertiary { self.check(16, 32) }
  42. method quaternary { self.check(64, 128) }
  43. }
  44. Rakudo::Internals.REGISTER-DYNAMIC: '$*COLLATION', {
  45. PROCESS::<$COLLATION> := Collation.new;
  46. }