<commit_before>//
//  SOS: the Stupid Operating System
//  by <NAME> (<EMAIL>)
//
//  Copyright (c) 2015 <NAME>
//  Released under the terms of the MIT license. See `LICENSE` in the root
//  directory of this repository for more information.
//
//! PS/2 keyboard driver

bitflags! {
    flags Modifiers: u8 { const L_SHIFT  = 0b1000_0000
                        , const R_SHIFT  = 0b0100_0000
                        , const SHIFT    = L_SHIFT.bits | R_SHIFT.bits
                        , const R_CTRL   = 0b0010_0000
                        , const L_CTRL   = 0b0001_0000
                        , const CTRL     = L_CTRL.bits | R_CTRL.bits
                        , const R_ALT    = 0b0000_1000
                        , const L_ALT    = 0b0000_0100
                        , const ALT      = L_ALT.bits | R_ALT.bits
                        , const CAPSLOCK = 0b0000_0010
                        }
}

impl Modifiers {
    /// Returns true if either shift key is pressed.
    #[inline] fn is_shifted(&self) -> bool {
        self.intersects(SHIFT)
    }

    /// Returns true if the keyboard's state is currently uppercase.
    #[inline] fn is_uppercase(&self) -> bool {
        self.is_shifted() ^ self.contains(CAPSLOCK)
    }

    /// Updates the modifiers state from a given scancode.
    fn update(&mut self, scancode: u8) {
        match scancode { 0x1D => self.insert(L_CTRL)
                       , 0x2A => self.insert(L_SHIFT)
                       , 0x36 => self.insert(R_SHIFT)
                       , 0x38 => self.insert(L_ALT)
                         // Caps lock toggles on leading edge
                       , 0x3A => self.toggle(CAPSLOCK)
                       , 0x9D => self.remove(L_CTRL)
                       , 0xAA => self.remove(L_SHIFT)
                       , 0xB6 => self.remove(R_SHIFT)
                       , 0xB8 => self.remove(L_ALT)
                       , _    => {}
        }
    }

    /// Apply the keyboard's modifiers to an ASCII scancode.
    fn apply_to_ascii(&self, ascii: u8) -> u8 {
        match ascii {
            b'a' ... b'z' if self.is_uppercase() => ascii - b'a' + b'A'
          , b'1' ... b'9' if self.is_shifted()   => ascii - b'1' + b'!'
          , b'0' if self.is_shifted()            => b')'
          , _ => ascii
        }
    }
}<commit_msg>More keyboard stuff<commit_after>//
//  SOS: the Stupid Operating System
//  by <NAME> (<EMAIL>)
//
//  Copyright (c) 2015 <NAME>
//  Released under the terms of the MIT license. See `LICENSE` in the root
//  directory of this repository for more information.
//
//! PS/2 keyboard driver

bitflags! {
    flags Modifiers: u8 { const L_SHIFT  = 0b1000_0000
                        , const R_SHIFT  = 0b0100_0000
                        , const SHIFT    = L_SHIFT.bits | R_SHIFT.bits
                        , const R_CTRL   = 0b0010_0000
                        , const L_CTRL   = 0b0001_0000
                        , const CTRL     = L_CTRL.bits | R_CTRL.bits
                        , const R_ALT    = 0b0000_1000
                        , const L_ALT    = 0b0000_0100
                        , const ALT      = L_ALT.bits | R_ALT.bits
                        , const CAPSLOCK = 0b0000_0010
                        , const NUMLOCK  = 0b0000_0001
                        }
}

impl Modifiers {
    /// Returns true if either shift key is pressed.
    #[inline] fn is_shifted(&self) -> bool {
        self.intersects(SHIFT)
    }

    /// Returns true if the keyboard's state is currently uppercase.
    #[inline] fn is_uppercase(&self) -> bool {
        self.is_shifted() ^ self.contains(CAPSLOCK)
    }

    /// Updates the modifiers state from a given scancode.
    fn update(&mut self, scancode: u8) {
        match scancode { 0x1D => self.insert(L_CTRL)
                       , 0x2A => self.insert(L_SHIFT)
                       , 0x36 => self.insert(R_SHIFT)
                       , 0x38 => self.insert(L_ALT)
                         // Caps lock toggles on leading edge
                       , 0x3A => self.toggle(CAPSLOCK)
                       , 0x9D => self.remove(L_CTRL)
                       , 0xAA => self.remove(L_SHIFT)
                       , 0xB6 => self.remove(R_SHIFT)
                       , 0xB8 => self.remove(L_ALT)
                       , _    => {}
        }
    }

    /// Apply the keyboard's modifiers to an ASCII scancode.
    fn modify(&self, ascii: u8) -> u8 {
        match ascii {
            b'a' ... b'z' if self.is_uppercase() => ascii - b'a' + b'A'
          , b'1' ... b'9' if self.is_shifted()   => ascii - b'1' + b'!'
          , b'0' if self.is_shifted()            => b')'
          , _ => ascii
        }
    }
}
{'fraction_non_alphanumeric': 0.10904425914047466, 'fraction_numerical': 0.04254864229206756, 'mean_word_length': 1.2965144820814924, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4967212', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1406, 'n_words': 357}