text:
//! Readline for Rust
//!
//! This implementation is based on [Antirez's
//! Linenoise](https://github.com/antirez/linenoise)
//!
//! # Example
//!
//! Usage
//!
//! ```
//! let mut rl = rustyline::Editor::<()>::new();
//! let readline = rl.readline(">> ");
//! match readline {
//!     Ok(line) => println!("Line: {:?}", line),
//!     Err(_) => println!("No input"),
//! }
//! ```
#![allow(unknown_lints)]
// #![feature(non_exhaustive)]
// #![feature(tool_lints)]

#[cfg(all(unix, not(any(target_os = "fuchsia"))))]
extern crate dirs;
extern crate libc;
#[macro_use]
extern crate log;
extern crate memchr;
#[cfg(all(unix, not(any(target_os = "fuchsia"))))]
extern crate nix;
extern crate unicode_segmentation;
extern crate unicode_width;
#[cfg(unix)]
extern crate utf8parse;
#[cfg(windows)]
extern crate winapi;
#[cfg(windows)]
extern crate kernel32;
#[cfg(target_os = "fuchsia")]
extern crate fuchsia_zircon as zx;
#[cfg(target_os = "fuchsia")]
extern crate fuchsia_device;

pub mod completion;
pub mod config;
mod edit;
pub mod error;
pub mod highlight;
pub mod hint;
pub mod history;
mod keymap;
mod keys;
mod kill_ring;
pub mod line_buffer;
mod undo;

mod tty;

use std::collections::HashMap;
use std::fmt;
use std::io::{self, Write};
use std::path::Path;
use std::result;
use std::sync::{Arc, Mutex, RwLock};
use unicode_width::UnicodeWidthStr;

use tty::{RawMode, RawReader, Renderer, Term, Terminal};

use completion::{longest_common_prefix, Candidate, Completer};
pub use config::{ColorMode, CompletionType, Config, EditMode, HistoryDuplicates};
use edit::State;
use highlight::Highlighter;
use hint::Hinter;
use history::{Direction, History};
pub use keymap::{Anchor, At, CharSearch, Cmd, Movement, RepeatCount, Word};
use keymap::{InputState, Refresher};
pub use keys::KeyPress;
use kill_ring::{KillRing, Mode};
use line_buffer::WordAction;

/// The error type for I/O and Linux Syscalls (Errno)
pub type Result<T> = result::Result<T, error::ReadlineError>;

/// Completes the line/word
fn complete_line<R: RawReader, C: Completer>(
    rdr: &mut R,
    s: &mut State,
    input_state: &mut InputState,
    completer: &C,
    highlighter: Option<&Highlighter>,
    config: &Config,
) -> Result<Option<Cmd>> {
    // get a list of completions
    let (start, candidates) = try!(completer.complete(&s.line, s.line.pos()));
    // if no completions, we are done
    if candidates.is_empty() {
        try!(s.out.beep());
        Ok(None)
    } else if CompletionType::Circular == config.completion_type() {
        let mark = s.changes.borrow_mut().begin();
        // Save the current edited line before overwriting it
        let backup = s.line.as_str().to_owned();
        let backup_pos = s.line.pos();
        let mut cmd;
        let mut i = 0;
        loop {
            // Show completion or original buffer
            if i < candidates.len() {
                let candidate = candidates[i].replacement();
                // TODO we can't highlight the line buffer directly
                /*let candidate = if let Some(highlighter) = s.highlighter {
                    highlighter.highlight_candidate(candidate, CompletionType::Circular)
                } else {
                    Borrowed(candidate)
                };*/
                completer.update(&mut s.line, start, candidate);
                try!(s.refresh_line());
            } else {
                // Restore current edited line
                s.line.update(&backup, backup_pos);
                try!(s.refresh_line());
            }

            cmd = try!(s.next_cmd(input_state, rdr, true));
            match cmd {
                Cmd::Complete => {
                    i = (i + 1) % (candidates.len() + 1); // Circular
                    if i == candidates.len() {
                        try!(s.out.beep());
                    }
                }
                Cmd::Abort => {
                    // Re-show original buffer
                    if i < candidates.len() {
                        s.line.update(&backup, backup_pos);
                        try!(s.refresh_line());
                    }
                    s.changes.borrow_mut().truncate(mark);
                    return Ok(None);
                }
                _ => {
                    s.changes.borrow_mut().end();
                    break;
                }
            }
        }
        Ok(Some(cmd))
    } else if CompletionType::List == config.completion_type() {
        if let Some(lcp) = longest_common_prefix(&candidates) {
            // if we can extend the item, extend it
            if lcp.len() > s.line.pos() - start {
                completer.update(&mut s.line, start, lcp);
                try!(s.refresh_line());
            }
        }
        // beep if ambiguous
        if candidates.len() > 1 {
            try!(s.out.beep());
        } else {
            return Ok(None);
        }
        // we can't complete any further, wait for second tab
        let mut cmd = try!(s.next_cmd(input_state, rdr, true));
        // if any character other than tab, pass it to the main loop
        if cmd != Cmd::Complete {
            return Ok(Some(cmd));
        }
        // move cursor to EOL to avoid overwriting the command line
        let save_pos = s.line.pos();
        try!(s.edit_move_end());
        s.line.set_pos(save_pos);
        // we got a second tab, maybe show list of possible completions
        let show_completions = if candidates.len() > config.completion_prompt_limit() {
            let msg = format!("\nDisplay all {} possibilities? (y or n)", candidates.len());
            try!(s.out.write_and_flush(msg.as_bytes()));
            s.old_rows += 1;
            while cmd != Cmd::SelfInsert(1, 'y')
                && cmd != Cmd::SelfInsert(1, 'Y')
                && cmd != Cmd::SelfInsert(1, 'n')
                && cmd != Cmd::SelfInsert(1, 'N')
                && cmd != Cmd::Kill(Movement::BackwardChar(1))
            {
                cmd = try!(s.next_cmd(input_state, rdr, false));
            }
            match cmd {
                Cmd::SelfInsert(1, 'y') | Cmd::SelfInsert(1, 'Y') => true,
                _ => false,
            }
        } else {
            true
        };
        if show_completions {
            page_completions(rdr, s, input_state, highlighter, &candidates)
        } else {
            try!(s.refresh_line());
            Ok(None)
        }
    } else {
        Ok(None)
    }
}

fn page_completions<R: RawReader, C: Candidate>(
    rdr: &mut R,
    s: &mut State,
    input_state: &mut InputState,
    highlighter: Option<&Highlighter>,
    candidates: &[C],
) -> Result<Option<Cmd>> {
    use std::cmp;

    let min_col_pad = 2;
    let cols = s.out.get_columns();
    let max_width = cmp::min(
        cols,
        candidates
            .into_iter()
            .map(|s| s.display().width())
            .max()
            .unwrap()
            + min_col_pad,
    );
    let num_cols = cols / max_width;

    let mut pause_row = s.out.get_rows() - 1;
    let num_rows = (candidates.len() + num_cols - 1) / num_cols;
    let mut ab = String::new();
    for row in 0..num_rows {
        if row == pause_row {
            try!(s.out.write_and_flush(b"\n--More--"));
            let mut cmd = Cmd::Noop;
            while cmd != Cmd::SelfInsert(1, 'y')
                && cmd != Cmd::SelfInsert(1, 'Y')
                && cmd != Cmd::SelfInsert(1, 'n')
                && cmd != Cmd::SelfInsert(1, 'N')
                && cmd != Cmd::SelfInsert(1, 'q')
                && cmd != Cmd::SelfInsert(1, 'Q')
                && cmd != Cmd::SelfInsert(1, ' ')
                && cmd != Cmd::Kill(Movement::BackwardChar(1))
                && cmd != Cmd::AcceptLine
            {
                cmd = try!(s.next_cmd(input_state, rdr, false));
            }
            match cmd {
                Cmd::SelfInsert(1, 'y') | Cmd::SelfInsert(1, 'Y') | Cmd::SelfInsert(1, ' ') => {
                    pause_row += s.out.get_rows() - 1;
                }
                Cmd::AcceptLine => {
                    pause_row += 1;
                }
                _ => break,
            }
            try!(s.out.write_and_flush(b"\n"));
        } else {
            try!(s.out.write_and_flush(b"\n"));
        }
        ab.clear();
        for col in 0..num_cols {
            let i = (col * num_rows) + row;
            if i < candidates.len() {
                let candidate = &candidates[i].display();
                let width = candidate.width();
                if let Some(highlighter) = highlighter {
                    ab.push_str(&highlighter.highlight_candidate(candidate, CompletionType::List));
                } else {
                    ab.push_str(candidate);
                }
                if ((col + 1) * num_rows) + row < candidates.len() {
                    for _ in width..max_width {
                        ab.push(' ');
                    }
                }
            }
        }
        try!(s.out.write_and_flush(ab.as_bytes()));
    }
    try!(s.out.write_and_flush(b"\n"));
    try!(s.refresh_line());
    Ok(None)
}

/// Incremental search
fn reverse_incremental_search<R: RawReader>(
    rdr: &mut R,
    s: &mut State,
    input_state: &mut InputState,
    history: &History,
) -> Result<Option<Cmd>> {
    if history.is_empty() {
        return Ok(None);
    }
    let mark = s.changes.borrow_mut().begin();
    // Save the current edited line (and cursor position) before overwriting it
    let backup = s.line.as_str().to_owned();
    let backup_pos = s.line.pos();

    let mut search_buf = String::new();
    let mut history_idx = history.len() - 1;
    let mut direction = Direction::Reverse;
    let mut success = true;

    let mut cmd;
    // Display the reverse-i-search prompt and process chars
    loop {
        let prompt = if success {
            format!("(reverse-i-search)`{}': ", search_buf)
        } else {
            format!("(failed reverse-i-search)`{}': ", search_buf)
        };
        try!(s.refresh_prompt_and_line(&prompt));

        cmd = try!(s.next_cmd(input_state, rdr, true));
        if let Cmd::SelfInsert(_, c) = cmd {
            search_buf.push(c);
        } else {
            match cmd {
                Cmd::Kill(Movement::BackwardChar(_)) => {
                    search_buf.pop();
                    continue;
                }
                Cmd::ReverseSearchHistory => {
                    direction = Direction::Reverse;
                    if history_idx > 0 {
                        history_idx -= 1;
                    } else {
                        success = false;
                        continue;
                    }
                }
                Cmd::ForwardSearchHistory => {
                    direction = Direction::Forward;
                    if history_idx < history.len() - 1 {
                        history_idx += 1;
                    } else {
                        success = false;
                        continue;
                    }
                }
                Cmd::Abort => {
                    // Restore current edited line (before search)
                    s.line.update(&backup, backup_pos);
                    try!(s.refresh_line());
                    s.changes.borrow_mut().truncate(mark);
                    return Ok(None);
                }
                Cmd::Move(_) => {
                    try!(s.refresh_line()); // restore prompt
                    break;
                }
                _ => break,
            }
        }
        success = match history.search(&search_buf, history_idx, direction) {
            Some(idx) => {
                history_idx = idx;
                let entry = history.get(idx).unwrap();
                let pos = entry.find(&search_buf).unwrap();
                s.line.update(entry, pos);
                true
            }
            _ => false,
        };
    }
    s.changes.borrow_mut().end();
    Ok(Some(cmd))
}

/// Handles reading and editting the readline buffer.
/// It will also handle special inputs in an appropriate fashion
/// (e.g., C-c will exit readline)
fn readline_edit<H: Helper>(
    prompt: &str,
    initial: Option<(&str, &str)>,
    editor: &mut Editor<H>,
    original_mode: &tty::Mode,
) -> Result<String> {
    let completer = editor.helper.as_ref();
    let hinter = editor.helper.as_ref().map(|h| h as &Hinter);
    let highlighter = if editor.term.colors_enabled() {
        editor.helper.as_ref().map(|h| h as &Highlighter)
    } else {
        None
    };

    let mut stdout = editor.term.create_writer();

    editor.reset_kill_ring(); // TODO recreate a new kill ring vs Arc<Mutex<KillRing>>
    let mut s = State::new(
        &mut stdout,
        prompt,
        editor.history.len(),
        hinter,
        highlighter,
    );
    let mut input_state = InputState::new(&editor.config, Arc::clone(&editor.custom_bindings));

    s.line.set_delete_listener(editor.kill_ring.clone());
    s.line.set_change_listener(s.changes.clone());

    if let Some((left, right)) = initial {
        s.line
            .update((left.to_owned() + right).as_ref(), left.len());
    }

    try!(s.refresh_line());

    let mut rdr = try!(editor.term.create_reader(&editor.config));

    loop {
        let rc = s.next_cmd(&mut input_state, &mut rdr, false);
        let mut cmd = try!(rc);

        if cmd.should_reset_kill_ring() {
            editor.reset_kill_ring();
        }

        // autocomplete
        if cmd == Cmd::Complete && completer.is_some() {
            let next = try!(complete_line(
                &mut rdr,
                &mut s,
                &mut input_state,
                completer.unwrap(),
                highlighter,
                &editor.config,
            ));
            if next.is_some() {
                cmd = next.unwrap();
            } else {
                continue;
            }
        }

        if let Cmd::SelfInsert(n, c) = cmd {
            try!(s.edit_insert(c, n));
            continue;
        } else if let Cmd::Insert(n, text) = cmd {
            try!(s.edit_yank(&input_state, &text, Anchor::Before, n));
            continue;
        }

        if cmd == Cmd::ReverseSearchHistory {
            // Search history backward
            let next = try!(reverse_incremental_search(
                &mut rdr,
                &mut s,
                &mut input_state,
                &editor.history,
            ));
            if next.is_some() {
                cmd = next.unwrap();
            } else {
                continue;
            }
        }

        match cmd {
            Cmd::Move(Movement::BeginningOfLine) => {
                // Move to the beginning of line.
                try!(s.edit_move_home())
            }
            Cmd::Move(Movement::ViFirstPrint) => {
                try!(s.edit_move_home());
                try!(s.edit_move_to_next_word(At::Start, Word::Big, 1))
            }
            Cmd::Move(Movement::BackwardChar(n)) => {
                // Move back a character.
                try!(s.edit_move_backward(n))
            }
            Cmd::ReplaceChar(n, c) => try!(s.edit_replace_char(c, n)),
            Cmd::Replace(mvt, text) => {
                try!(s.edit_kill(&mvt));
                if let Some(text) = text {
                    try!(s.edit_insert_text(&text))
                }
            }
            Cmd::Overwrite(c) => {
                try!(s.edit_overwrite_char(c));
            }
            Cmd::EndOfFile => {
                if !input_state.is_emacs_mode() && !s.line.is_empty() {
                    try!(s.edit_move_end());
                    break;
                } else if s.line.is_empty() {
                    return Err(error::ReadlineError::Eof);
                } else {
                    try!(s.edit_delete(1))
                }
            }
            Cmd::Move(Movement::EndOfLine) => {
                // Move to the end of line.
                try!(s.edit_move_end())
            }
            Cmd::Move(Movement::ForwardChar(n)) => {
                // Move forward a character.
                try!(s.edit_move_forward(n))
            }
            Cmd::ClearScreen => {
                // Clear the screen leaving the current line at the top of the screen.
                try!(s.out.clear_screen());
                try!(s.refresh_line())
            }
            Cmd::NextHistory => {
                // Fetch the next command from the history list.
                try!(s.edit_history_next(&editor.history, false))
            }
            Cmd::PreviousHistory => {
                // Fetch the previous command from the history list.
                try!(s.edit_history_next(&editor.history, true))
            }
            Cmd::HistorySearchBackward => {
                try!(s.edit_history_search(&editor.history, Direction::Reverse))
            }
            Cmd::HistorySearchForward => {
                try!(s.edit_history_search(&editor.history, Direction::Forward))
            }
            Cmd::TransposeChars => {
                // Exchange the char before cursor with the character at cursor.
                try!(s.edit_transpose_chars())
            }
            #[cfg(unix)]
            Cmd::QuotedInsert => {
                // Quoted insert
                let c = try!(rdr.next_char());
                try!(s.edit_insert(c, 1)) // FIXME
            }
            Cmd::Yank(n, anchor) => {
                // retrieve (yank) last item killed
                let mut kill_ring = editor.kill_ring.lock().unwrap();
                if let Some(text) = kill_ring.yank() {
                    try!(s.edit_yank(&input_state, text, anchor, n))
                }
            }
            Cmd::ViYankTo(ref mvt) => {
                if let Some(text) = s.line.copy(mvt) {
                    let mut kill_ring = editor.kill_ring.lock().unwrap();
                    kill_ring.kill(&text, Mode::Append)
                }
            }
            // TODO CTRL-_ // undo
            Cmd::AcceptLine => {
                #[cfg(test)]
                {
                    editor.term.cursor = s.cursor.col;
                }
                // Accept the line regardless of where the cursor is.
                try!(s.edit_move_end());
                if s.hinter.is_some() {
                    // Force a refresh without hints to leave the previous
                    // line as the user typed it after a newline.
                    s.hinter = None;
                    try!(s.refresh_line());
                }
                break;
            }
            Cmd::BeginningOfHistory => {
                // move to first entry in history
                try!(s.edit_history(&editor.history, true))
            }
            Cmd::EndOfHistory => {
                // move to last entry in history
                try!(s.edit_history(&editor.history, false))
            }
            Cmd::Move(Movement::BackwardWord(n, word_def)) => {
                // move backwards one word
                try!(s.edit_move_to_prev_word(word_def, n))
            }
            Cmd::CapitalizeWord => {
                // capitalize word after point
                try!(s.edit_word(WordAction::CAPITALIZE))
            }
            Cmd::Kill(ref mvt) => {
                try!(s.edit_kill(mvt));
            }
            Cmd::Move(Movement::ForwardWord(n, at, word_def)) => {
                // move forwards one word
                try!(s.edit_move_to_next_word(at, word_def, n))
            }
            Cmd::DowncaseWord => {
                // lowercase word after point
                try!(s.edit_word(WordAction::LOWERCASE))
            }
            Cmd::TransposeWords(n) => {
                // transpose words
                try!(s.edit_transpose_words(n))
            }
            Cmd::UpcaseWord => {
                // uppercase word after point
                try!(s.edit_word(WordAction::UPPERCASE))
            }
            Cmd::YankPop => {
                // yank-pop
                let mut kill_ring = editor.kill_ring.lock().unwrap();
                if let Some((yank_size, text)) = kill_ring.yank_pop() {
                    try!(s.edit_yank_pop(yank_size, text))
                }
            }
            Cmd::Move(Movement::ViCharSearch(n, cs)) => try!(s.edit_move_to(cs, n)),
            Cmd::Undo(n) => {
                s.line.remove_change_listener();
                if s.changes.borrow_mut().undo(&mut s.line, n) {
                    try!(s.refresh_line());
                }
                s.line.set_change_listener(s.changes.clone());
            }
            Cmd::Interrupt => {
                return Err(error::ReadlineError::Interrupted);
            }
            #[cfg(all(unix, not(any(target_os = "fuchsia"))))]
            Cmd::Suspend => {
                try!(original_mode.disable_raw_mode());
                try!(tty::suspend());
                try!(editor.term.enable_raw_mode()); // TODO original_mode may have changed
                try!(s.refresh_line());
                continue;
            }
            Cmd::Noop => {}
            _ => {
                // Ignore the character typed.
            }
        }
    }
    if cfg!(windows) {
        let _ = original_mode; // silent warning
    }
    Ok(s.line.into_string())
}

struct Guard<'m>(&'m tty::Mode);

#[allow(unused_must_use)]
impl<'m> Drop for Guard<'m> {
    fn drop(&mut self) {
        let Guard(mode) = *self;
        mode.disable_raw_mode();
    }
}

/// Readline method that will enable RAW mode, call the `readline_edit()`
/// method and disable raw mode
fn readline_raw<H: Helper>(
    prompt: &str,
    initial: Option<(&str, &str)>,
    editor: &mut Editor<H>,
) -> Result<String> {
    let original_mode = try!(editor.term.enable_raw_mode());
    let guard = Guard(&original_mode);
    let user_input = readline_edit(prompt, initial, editor, &original_mode);
    if editor.config.auto_add_history() {
        if let Ok(ref line) = user_input {
            editor.add_history_entry(line.as_ref());
        }
    }
    drop(guard); // try!(disable_raw_mode(original_mode));
    editor
        .term
        .create_writer()
        .write_and_flush(b"\n")
        .unwrap();
    user_input
}

fn readline_direct() -> Result<String> {
    let mut line = String::new();
    if try!(io::stdin().read_line(&mut line)) > 0 {
        Ok(line)
    } else {
        Err(error::ReadlineError::Eof)
    }
}

/// Syntax specific helper.
///
/// TODO Tokenizer/parser used for both completion, suggestion, highlighting.
/// (parse current line once)
pub trait Helper
where
    Self: Completer,
    Self: Hinter,
    Self: Highlighter,
{
}

impl Helper for () {}

/// Line editor
pub struct Editor<H: Helper> {
    term: Terminal,
    history: History,
    helper: Option<H>,
    kill_ring: Arc<Mutex<KillRing>>,
    config: Config,
    custom_bindings: Arc<RwLock<HashMap<KeyPress, Cmd>>>,
}

//#[allow(clippy::new_without_default)]
impl<H: Helper> Editor<H> {
    /// Create an editor with the default configuration
    pub fn new() -> Editor<H> {
        Self::with_config(Config::default())
    }

    /// Create an editor with a specific configuration.
    pub fn with_config(config: Config) -> Editor<H> {
        let term = Terminal::new(config.color_mode(), config.output_stream());
        Editor {
            term,
            history: History::with_config(config),
            helper: None,
            kill_ring: Arc::new(Mutex::new(KillRing::new(60))),
            config,
            custom_bindings: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// This method will read a line from STDIN and will display a `prompt`.
    ///
    /// It uses terminal-style interaction if `stdin` is connected to a
    /// terminal.
    /// Otherwise (e.g., if `stdin` is a pipe or the terminal is not supported),
    /// it uses file-style interaction.
    pub fn readline(&mut self, prompt: &str) -> Result<String> {
        self.readline_with(prompt, None)
    }

    /// This function behaves in the exact same manner as `readline`, except
    /// that it pre-populates the input area.
    ///
    /// The text that resides in the input area is given as a 2-tuple.
    /// The string on the left of the tuple is what will appear to the left of
    /// the cursor and the string on the right is what will appear to the
    /// right of the cursor.
    pub fn readline_with_initial(&mut self, prompt: &str, initial: (&str, &str)) -> Result<String> {
        self.readline_with(prompt, Some(initial))
    }

    fn readline_with(&mut self, prompt: &str, initial: Option<(&str, &str)>) -> Result<String> {
        if self.term.is_unsupported() {
            debug!(target: "rustyline", "unsupported terminal");
            // Write prompt and flush it to stdout
            let mut stdout = io::stdout();
            try!(stdout.write_all(prompt.as_bytes()));
            try!(stdout.flush());

            readline_direct()
        } else if !self.term.is_stdin_tty() {
            debug!(target: "rustyline", "stdin is not a tty");
            // Not a tty: read from file / pipe.
            readline_direct()
        } else {
            readline_raw(prompt, initial, self)
        }
    }

    /// Load the history from the specified file.
    pub fn load_history<P: AsRef<Path> + ?Sized>(&mut self, path: &P) -> Result<()> {
        self.history.load(path)
    }

    /// Save the history in the specified file.
    pub fn save_history<P: AsRef<Path> + ?Sized>(&self, path: &P) -> Result<()> {
        self.history.save(path)
    }

    /// Add a new entry in the history.
    pub fn add_history_entry<S: AsRef<str> + Into<String>>(&mut self, line: S) -> bool {
        self.history.add(line)
    }

    /// Clear history.
    pub fn clear_history(&mut self) {
        self.history.clear()
    }

    /// Return a mutable reference to the history object.
    pub fn history_mut(&mut self) -> &mut History {
        &mut self.history
    }

    /// Return an immutable reference to the history object.
    pub fn history(&self) -> &History {
        &self.history
    }

    /// Register a callback function to be called for tab-completion
    /// or to show hints to the user at the right of the prompt.
    pub fn set_helper(&mut self, helper: Option<H>) {
        self.helper = helper;
    }

    /// Return a mutable reference to the helper.
    pub fn helper_mut(&mut self) -> Option<&mut H> {
        self.helper.as_mut()
    }

    /// Return an immutable reference to the helper.
    pub fn helper(&self) -> Option<&H> {
        self.helper.as_ref()
    }

    /// Bind a sequence to a command.
    pub fn bind_sequence(&mut self, key_seq: KeyPress, cmd: Cmd) -> Option<Cmd> {
        let mut bindings = self.custom_bindings.write().unwrap();
        bindings.insert(key_seq, cmd)
    }

    /// Remove a binding for the given sequence.
    pub fn unbind_sequence(&mut self, key_seq: KeyPress) -> Option<Cmd> {
        let mut bindings = self.custom_bindings.write().unwrap();
        bindings.remove(&key_seq)
    }

    /// ```
    /// let mut rl = rustyline::Editor::<()>::new();
    /// for readline in rl.iter("> ") {
    ///     match readline {
    ///         Ok(line) => {
    ///             println!("Line: {}", line);
    ///         }
    ///         Err(err) => {
    ///             println!("Error: {:?}", err);
    ///             break;
    ///         }
    ///     }
    /// }
    /// ```
    pub fn iter<'a>(&'a mut self, prompt: &'a str) -> Iter<H> {
        Iter {
            editor: self,
            prompt,
        }
    }

    fn reset_kill_ring(&self) {
        let mut kill_ring = self.kill_ring.lock().unwrap();
        kill_ring.reset();
    }
}

impl<H: Helper> config::Configurer for Editor<H> {
    fn config_mut(&mut self) -> &mut Config {
        &mut self.config
    }

    fn set_max_history_size(&mut self, max_size: usize) {
        self.config_mut().set_max_history_size(max_size);
        self.history.set_max_len(max_size);
    }

    fn set_history_ignore_dups(&mut self, yes: bool) {
        self.config_mut().set_history_ignore_dups(yes);
        self.history.ignore_dups = yes;
    }

    fn set_history_ignore_space(&mut self, yes: bool) {
        self.config_mut().set_history_ignore_space(yes);
        self.history.ignore_space = yes;
    }

    fn set_color_mode(&mut self, color_mode: ColorMode) {
        self.config_mut().set_color_mode(color_mode);
        self.term.color_mode = color_mode;
    }
}

impl<H: Helper> fmt::Debug for Editor<H> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Editor")
            .field("term", &self.term)
            .field("config", &self.config)
            .finish()
    }
}

/// Edited lines iterator
pub struct Iter<'a, H: Helper>
where
    H: 'a,
{
    editor: &'a mut Editor<H>,
    prompt: &'a str,
}

impl<'a, H: Helper> Iterator for Iter<'a, H> {
    type Item = Result<String>;

    fn next(&mut self) -> Option<Result<String>> {
        let readline = self.editor.readline(self.prompt);
        match readline {
            Ok(l) => Some(Ok(l)),
            Err(error::ReadlineError::Eof) => None,
            e @ Err(_) => Some(e),
        }
    }
}

enum StdStream {
    Stdout(io::Stdout),
    Stderr(io::Stderr),
}
impl StdStream {
    fn from_stream_type(t: config::OutputStreamType) -> StdStream {
        match t {
            config::OutputStreamType::Stderr => StdStream::Stderr(io::stderr()),
            config::OutputStreamType::Stdout => StdStream::Stdout(io::stdout()),
        }
    }
}
#[cfg(unix)]
impl std::os::unix::io::AsRawFd for StdStream {
    fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
        match self {
            StdStream::Stdout(e) => e.as_raw_fd(),
            StdStream::Stderr(e) => e.as_raw_fd(),
        }
    }
}
impl io::Write for StdStream {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        match self {
            StdStream::Stdout(ref mut e) => e.write(buf),
            StdStream::Stderr(ref mut e) => e.write(buf),
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        match self {
            StdStream::Stdout(ref mut e) => e.flush(),
            StdStream::Stderr(ref mut e) => e.flush(),
        }
    }

    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
        match self {
            StdStream::Stdout(ref mut e) => e.write_all(buf),
            StdStream::Stderr(ref mut e) => e.write_all(buf),
        }
    }

    fn write_fmt(&mut self, fmt: std::fmt::Arguments) -> io::Result<()> {
        match self {
            StdStream::Stdout(ref mut e) => e.write_fmt(fmt),
            StdStream::Stderr(ref mut e) => e.write_fmt(fmt),
        }
    }

    fn by_ref(&mut self) -> &mut StdStream {
        self
    }
}

#[cfg(test)]
#[macro_use]
extern crate assert_matches;
#[cfg(test)]
mod test;


text:
/**
 * RClassTable.h
 * ClassTable for manage(get info, etc.) instances in runtime.
 * Author Kucheruavyu Ilya (kojiba@ro.ru)
 * 2014 Ukraine Kharkiv
 *  _         _ _ _
 * | |       (_|_) |
 * | | _____  _ _| |__   __ _
 * | |/ / _ \| | | '_ \ / _` |
 * |   < (_) | | | |_) | (_| |
 * |_|\_\___/| |_|_.__/ \__,_|
 *          _/ |
 *         |__/
 **/

#ifndef __CLASS_TABLE_H__
#define __CLASS_TABLE_H__

#include <RSyntax.h>
#include <RClassNamePair.h>
#include <RArray.h>

#if defined(RAY_CLASS_TABLE_THREAD_SAFE) && !defined(RAY_ARRAY_THREAD_SAFE)
    #include <RThread.h>
#endif

class(RClassTable) //------------------------------------------------------------
    discipleOf(RArray)
    discipleOf(RCompareDelegate)

#if defined(RAY_CLASS_TABLE_THREAD_SAFE)
    RMutex mutex;
#endif
endOf(RClassTable) //-------------------------------------------------------

constructor (RClassTable));
destructor  (RClassTable);
printer     (RClassTable);
singleton   (RClassTable);

method(size_t,      registerClassWithName,    RClassTable),    char *name); // strings must be constant, or self-cleanup
method(size_t,      getIdentifierByClassName, RClassTable),    char *name); // strings must be constant, or self-cleanup
method(RCString*,   getClassNameByIdentifier, RClassTable),    size_t id);
method(size_t,      getNumberOfClasses,       RClassTable));

//----------------------------------------------------------------------------------

#define makeRCTable() $(nil, c(RClassTable)) );

// some substitutions macro for better syntax
#if defined(RCTSingleton) || defined(registerClassOnce) || defined(printRCTS) \
    || defined(releaseRCTS) || defined(flushRCTS) || defined(getIdentifierByName)
    #error "Ray Class Table Defines Error - some already defined"
#else
#define RCTSingleton              singletonCall(RClassTable)
#define flushRCTS()               $(master(RCTSingleton, RArray), m(flush, RArray)));
#define releaseRCTS               d(RClassTable)(RCTSingleton); free(singletonCall(RClassTable))
#define registerClassOnce(name)   $(RCTSingleton, m(registerClassWithName, RClassTable)), name) // return id, or 0 if fails. [0; 3] ids is reserved
#define printRCTS                 $(RCTSingleton, p(RClassTable)) )
#define getIdentifierByName(name) $(RCTSingleton, m(getIdentifierByClassName, RClassTable)), name)
#endif

#endif /*__CLASS_TABLE_H__*/

redpajama_set_name: RedPajamaGithub


text:
#include <simdmath/_llabsi2.h>

vector signed long long
llabsi2 (vector signed long long in)
{
  return _llabsi2(in);
}


text:
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <bmx/mxf_helper/MXFDescriptorHelper.h>
#include <bmx/mxf_helper/PictureMXFDescriptorHelper.h>
#include <bmx/mxf_helper/SoundMXFDescriptorHelper.h>
#include <bmx/mxf_helper/DataMXFDescriptorHelper.h>
#include <bmx/Utils.h>
#include <bmx/BMXTypes.h>
#include <bmx/BMXException.h>
#include <bmx/Logging.h>

#include <mxf/mxf_avid_labels_and_keys.h>

using namespace std;
using namespace bmx;
using namespace mxfpp;

EssenceType MXFDescriptorHelper::IsSupported(mxfpp::FileDescriptor *file_descriptor, mxfUL alternative_ec_label)
{
    EssenceType essence_type = PictureMXFDescriptorHelper::IsSupported(file_descriptor, alternative_ec_label);
    if (essence_type)
        return essence_type;
    essence_type = SoundMXFDescriptorHelper::IsSupported(file_descriptor, alternative_ec_label);
    if (essence_type)
        return essence_type;
    essence_type = DataMXFDescriptorHelper::IsSupported(file_descriptor, alternative_ec_label);
    return essence_type;
}

MXFDescriptorHelper* MXFDescriptorHelper::Create(mxfpp::FileDescriptor *file_descriptor, uint16_t mxf_version,
                                                 mxfUL alternative_ec_label)
{
    if (PictureMXFDescriptorHelper::IsSupported(file_descriptor, alternative_ec_label))
        return PictureMXFDescriptorHelper::Create(file_descriptor, mxf_version, alternative_ec_label);
    else if (SoundMXFDescriptorHelper::IsSupported(file_descriptor, alternative_ec_label))
        return SoundMXFDescriptorHelper::Create(file_descriptor, mxf_version, alternative_ec_label);
    else if (DataMXFDescriptorHelper::IsSupported(file_descriptor, alternative_ec_label))
        return DataMXFDescriptorHelper::Create(file_descriptor, mxf_version, alternative_ec_label);

    BMX_ASSERT(false);
    return 0;
}

MXFDescriptorHelper* MXFDescriptorHelper::Create(EssenceType essence_type)
{
    if (PictureMXFDescriptorHelper::IsSupported(essence_type))
        return PictureMXFDescriptorHelper::Create(essence_type);
    else if (SoundMXFDescriptorHelper::IsSupported(essence_type))
        return SoundMXFDescriptorHelper::Create(essence_type);
    else if (DataMXFDescriptorHelper::IsSupported(essence_type))
        return DataMXFDescriptorHelper::Create(essence_type);

    BMX_ASSERT(false);
    return 0;
}

bool MXFDescriptorHelper::CompareECULs(mxfUL ec_label_a, mxfUL alternative_ec_label_a, mxfUL ec_label_b)
{
    return mxf_equals_ul_mod_regver(&ec_label_a, &ec_label_b) ||
           mxf_equals_ul_mod_regver(&alternative_ec_label_a, &ec_label_b);
}

bool MXFDescriptorHelper::IsNullAvidECUL(mxfUL ec_label, mxfUL alternative_ec_label)
{
    return mxf_equals_ul_mod_regver(&ec_label, &MXF_EC_L(AvidAAFKLVEssenceContainer)) &&
           mxf_equals_ul(&alternative_ec_label, &g_Null_UL);
}

MXFDescriptorHelper::MXFDescriptorHelper()
{
    mSampleRate = FRAME_RATE_25;
    mFrameWrapped = true;
    mFileDescriptor = 0;
    mFlavour = MXFDESC_SMPTE_377_2004_FLAVOUR;

    // mEssenceType is set by subclass
}

MXFDescriptorHelper::~MXFDescriptorHelper()
{
}

void MXFDescriptorHelper::Initialize(FileDescriptor *file_descriptor, uint16_t mxf_version, mxfUL alternative_ec_label)
{
    (void)mxf_version;
    (void)alternative_ec_label;

    mSampleRate = normalize_rate(file_descriptor->getSampleRate());
    // mEssenceType and mFrameWrapped are set by subclass

    mFileDescriptor = file_descriptor;
}

void MXFDescriptorHelper::SetEssenceType(EssenceType essence_type)
{
    BMX_ASSERT(!mFileDescriptor);

    mEssenceType = essence_type;
}

void MXFDescriptorHelper::SetSampleRate(mxfRational sample_rate)
{
    BMX_ASSERT(!mFileDescriptor);
    BMX_CHECK(sample_rate == FRAME_RATE_23976 ||
              sample_rate == FRAME_RATE_24 ||
              sample_rate == FRAME_RATE_25 ||
              sample_rate == FRAME_RATE_2997 ||
              sample_rate == FRAME_RATE_30 ||
              sample_rate == FRAME_RATE_50 ||
              sample_rate == FRAME_RATE_5994 ||
              sample_rate == FRAME_RATE_60 ||
              (IsSound() && sample_rate == SAMPLING_RATE_48K));

    mSampleRate = sample_rate;
}

void MXFDescriptorHelper::SetFrameWrapped(bool frame_wrapped)
{
    BMX_ASSERT(!mFileDescriptor);

    mFrameWrapped = frame_wrapped;
}

void MXFDescriptorHelper::SetFlavour(int flavour)
{
    BMX_ASSERT(!mFileDescriptor);

    mFlavour = flavour;
    if ((flavour & MXFDESC_AVID_FLAVOUR))
        mFrameWrapped = false;
}

void MXFDescriptorHelper::UpdateFileDescriptor()
{
    mFileDescriptor->setEssenceContainer(ChooseEssenceContainerUL());
    mFileDescriptor->setSampleRate(mSampleRate);
}

mxfUL MXFDescriptorHelper::GetEssenceContainerUL() const
{
    if (mFileDescriptor)
        return mFileDescriptor->getEssenceContainer();
    else
        return ChooseEssenceContainerUL();
}


text:
/* global File */
import includes from 'lodash.includes';

/* Validation */
export default (types, message) =>
  value => ({
    isValid: (value instanceof Array || value instanceof Object) &&
      value[0] instanceof File &&
      includes(types, value[0].type),
    message: message || 'Invalid file type',
  });


text:
#ifndef __GUMMI_GUI_MAIN_H__
#define __GUMMI_GUI_MAIN_H__

#include <glib.h>
#include <gtk/gtk.h>

#include "gui-import.h"
#include "gui-prefs.h"
#include "gui-preview.h"
#include "gui-search.h"
#include "gui-snippets.h"
#include "gui-tabmanager.h"
#include "gui-infoscreen.h"
#include "gui-menu.h"
#include "gui-project.h"

#define RECENT_FILES_NUM 5
#define TEXCOUNT_OUTPUT_LINES 7

/* These macros should be only used in GUI related classes
 * which acted as syntax sugar */
/* These macros should be only used in GUI related classes
 * which acted as syntax sugar */
#define g_e_buffer GTK_TEXT_BUFFER (g_active_editor->buffer)
#define g_e_view GTK_TEXT_VIEW (g_active_editor->view)
#define g_active_tab gummi->tabmanager->active_tab
#define g_active_editor gummi->tabmanager->active_editor
#define g_tabs gummi->tabmanager->tabs

#define GUMMI_GUI(x) ((GummiGui*)x)
typedef struct _GummiGui GummiGui;

struct _GummiGui {
    GuMenuGui* menugui;
    GuImportGui* importgui;
    GuPrefsGui* prefsgui;
    GuPreviewGui* previewgui;
    GuSearchGui* searchgui;
    GuSnippetsGui* snippetsgui;
    GuTabmanagerGui* tabmanagergui;
    GuInfoscreenGui* infoscreengui;
    GuProjectGui* projectgui;

    GtkWindow* mainwindow;
    GtkTextBuffer* errorbuff;
    GtkTextView* errorview;
    GtkBox* rightpane;

    GtkWidget* toolbar;
    GtkStatusbar* statusbar;
    GtkCheckMenuItem* menu_spelling;
    GtkCheckMenuItem* menu_snippets;
    GtkCheckMenuItem* menu_toolbar;
    GtkCheckMenuItem* menu_statusbar;
    GtkCheckMenuItem* menu_rightpane;
    GtkCheckMenuItem* menu_autosync;
    GtkMenuItem* recent[5];
    gint insens_widget_size;
    GtkWidget** insens_widgets;
    GtkBuilder *builder;
    GtkWidget *docstatswindow;
    GtkWidget *bibcompile;

    GtkMenuItem* menu_runbibtex;
    GtkMenuItem* menu_runmakeindex;

    guint statusid;
    gchar* recent_list[5];
};

typedef enum _GuFilterType {
    TYPE_LATEX = 0,
    TYPE_LATEX_SAVEAS,
    TYPE_PDF,
    TYPE_IMAGE,
    TYPE_BIBLIO,
    TYPE_PROJECT
} GuFilterType;

/* Main GUI */
GummiGui* gui_init (GtkBuilder* builder);
void gui_main (GtkBuilder* builder);
gboolean gui_quit (void);

void gui_set_filename_display (GuTabContext* tc,
                                        gboolean title, gboolean label);
void gui_set_window_title (const gchar* filename, const gchar* text);

void gui_open_file (const gchar* filename);
void gui_save_file (GuTabContext* tab, gboolean saveas);
void gui_set_hastabs_sensitive (gboolean enable);

void on_tab_notebook_switch_page (GtkNotebook *notebook, GtkWidget* nbpage, int pagenr, void* data);
void on_tool_textstyle_bold_activate (GtkWidget* widget, void* user);
void on_tool_textstyle_italic_activate (GtkWidget* widget, void* user);
void on_tool_textstyle_underline_activate (GtkWidget* widget, void* user);
void on_tool_textstyle_left_activate (GtkWidget* widget, void* user);
void on_tool_textstyle_center_activate (GtkWidget* widget, void* user);
void on_tool_textstyle_right_activate (GtkWidget* widget, void* user);
void on_button_template_add_clicked (GtkWidget* widget, void* user);
void on_button_template_remove_clicked (GtkWidget* widget, void* user);
void on_button_template_open_clicked (GtkWidget* widget, void* user);
void on_button_template_close_clicked (GtkWidget* widget, void* user);
void on_template_rowitem_edited (GtkWidget* widget, gchar *path, gchar* filenm,
        void* user);
void gui_recover_from_swapfile (const gchar* filename);
void on_menu_autosync_toggled (GtkCheckMenuItem *menu_autosync, void* user);

void on_button_biblio_compile_clicked (GtkWidget* widget, void* user);
void on_button_biblio_detect_clicked (GtkWidget* widget, void* user);
void on_bibreference_clicked (GtkTreeView* view, GtkTreePath* Path,
        GtkTreeViewColumn* column, void* user);
void on_biblio_filter_changed (GtkWidget* widget, void* user);
gboolean on_bibprogressbar_update (void* data);

void on_recovery_infobar_response (GtkInfoBar* bar, gint res, gpointer filename);
void gui_recovery_mode_enable (GuTabContext* tab, const gchar* filename);
void gui_recovery_mode_disable (GtkInfoBar *infobar);

/* misc functions */
gchar* get_open_filename (GuFilterType type);
gchar* get_save_filename (GuFilterType type);
void file_dialog_set_filter (GtkFileChooser* dialog, GuFilterType type);
gint check_for_save (GuEditor* editor);

void add_to_recent_list (const gchar* filename);
void display_recent_files (GummiGui* gui);

void gui_buildlog_set_text (const gchar *message);
void statusbar_set_message (const gchar* message);
gboolean statusbar_del_message (void* user);

void typesetter_setup (void);

void check_preview_timer (void);

#endif /* __GUMMI_GUI_MAIN_H__ */


text:
import {Exp, ExpInputs, KernelConfig, Tensor} from '@tensorflow/tfjs';

import {NodeJSKernelBackend} from '../nodejs_kernel_backend';

export const expConfig: KernelConfig = {
  kernelName: Exp,
  backendName: 'tensorflow',
  kernelFunc: (args) => {
    const {x} = args.inputs as ExpInputs;
    const backend = args.backend as NodeJSKernelBackend;

    let xTensor = x;
    if (x.dtype === 'int32') {
      xTensor = (x as Tensor).toFloat();
      (x as Tensor).dispose();
    }
    return backend.executeSingleInput(Exp, xTensor);
  }
};


text:
package com.adaptris.core.transform.schema;

import static com.adaptris.core.BaseCase.PROPERTIES;
import static com.adaptris.core.ServiceCase.execute;
import static com.adaptris.core.transform.XmlValidationServiceTest.KEY_INPUT_FILE;
import static com.adaptris.core.transform.XmlValidationServiceTest.KEY_WILL_NOT_VALIDATE;
import static com.adaptris.core.transform.XmlValidationServiceTest.KEY_WILL_VALIDATE_SCHEMA;
import org.junit.Test;
import com.adaptris.core.AdaptrisMessage;
import com.adaptris.core.ServiceException;
import com.adaptris.core.cache.ExpiringMapCache;
import com.adaptris.core.services.cache.CacheConnection;
import com.adaptris.core.stubs.DefectiveMessageFactory;
import com.adaptris.core.stubs.DefectiveMessageFactory.WhenToBreak;
import com.adaptris.core.stubs.MessageHelper;
import com.adaptris.core.transform.XmlValidationService;
import com.adaptris.core.util.LifecycleHelper;

public class BasicSchemaValidationTest {

  @Test
  public void testBasicSchemaValidator() throws Exception {
    String schemaUrl = PROPERTIES.getProperty(KEY_WILL_VALIDATE_SCHEMA);
    BasicXmlSchemaValidator validator = new BasicXmlSchemaValidator().withSchema(schemaUrl)
        .withSchemaCache(new CacheConnection().withCacheInstance(new ExpiringMapCache()));
    XmlValidationService service = new XmlValidationService(validator);
    try {
      LifecycleHelper.initAndStart(service);
      AdaptrisMessage m1 = MessageHelper.createMessage(PROPERTIES.getProperty(KEY_INPUT_FILE));
      AdaptrisMessage m2 = MessageHelper.createMessage(PROPERTIES.getProperty(KEY_INPUT_FILE));
      service.doService(m1);
      service.doService(m2);
    } finally {
      LifecycleHelper.stopAndClose(service);
    }
  }

  @Test(expected=ServiceException.class)
  public void testBasicSchemaValidator_Exception() throws Exception {
    String schemaUrl = PROPERTIES.getProperty(KEY_WILL_VALIDATE_SCHEMA);
    BasicXmlSchemaValidator validator = new BasicXmlSchemaValidator().withSchema(schemaUrl)
        .withSchemaCache(new CacheConnection().withCacheInstance(new ExpiringMapCache()));
    XmlValidationService service = new XmlValidationService(validator);
    try {
      LifecycleHelper.initAndStart(service);
      AdaptrisMessage m1 = new DefectiveMessageFactory(WhenToBreak.BOTH).newMessage();
      service.doService(m1);
    } finally {
      LifecycleHelper.stopAndClose(service);
    }
  }

  @Test(expected=ServiceException.class)
  public void testBasicSchemaValidator_Invalid() throws Exception {
    String schemaUrl = PROPERTIES.getProperty(KEY_WILL_NOT_VALIDATE);
    BasicXmlSchemaValidator validator = new BasicXmlSchemaValidator().withSchema(schemaUrl);
    AdaptrisMessage msg = MessageHelper.createMessage(PROPERTIES.getProperty(KEY_INPUT_FILE));
    XmlValidationService service = new XmlValidationService(validator);
    execute(service, msg);
  }

}


text:
package org.wso2.carbon.apimgt.impl.jwt;

import com.nimbusds.jwt.SignedJWT;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.impl.dto.JWTValidationInfo;

public interface JWTValidationService {

    public JWTValidationInfo validateJWTToken(SignedJWT signedJWT) throws APIManagementException;
    public String getKeyManagerNameIfJwtValidatorExist(SignedJWT signedJWT) throws APIManagementException;
}


text:
package com.wakatime.test

import com.alpha.SomeClass
import com.bravo.something.{User, UserPreferences}
import com.charlie.{Delta => Foxtrot}
import __root__.golf._
import com.hotel.india._
import juliett.kilo

object SimpleObject {
  def method(arg: String): String = {
    return "value"
  }
}


text:
"""
A transposition cipher we'll call the "matrix cipher" can be defined as follows: write each character in the text that
you want to encrypt in a matrix of some specified width, where the width is the key of the cipher. So, for instance, if
you wanted to encrypt "The cake is a lie!" with the key 3, you would write it like so (the spaces are replaced with
underscores for clarity):
T h e
_ c a
k e _
i s _
a _ l
i e !

Then to get the ciphertext, you simply read off the columns one by one. So in this case, the ciphertext would be "T_
kiaihces_eea__l!", or "T kiaihces eea  l!" if you put the spaces back in.
If the text doesn't fit the matrix perfectly, you add in random letters to fill in the last row. So, if we wanted to
encode "The cake is a lie!" with key 7, we'd construct this matrix:
T h e _ c a k
e _ i s _ a _
l i e ! v m z
Here "v", "m" and "z" have been added in to fill the last row, and the ciphertext is "Telh ieie s!c vaamk z".

Write an implementation of the matrix cipher that can both encode and decode text given the correct key.

BONUS: One of the major tricks code-crackers have used throughout history is the fact that the first parts of many
messages often follow a regular pattern. They start with "Hello" or "Greetings", "Transmission from" or something like
that (Allied codebreakers during World War II took advantage of the fact that Nazi messages often began with "Heil
Hitler").
Use this trick to construct a way to automatically crack messages encrypted with the matrix cipher. That is, given a
certain ciphertext to crack and the first few characters of the cleartext, figure out what the entire message is
without human input. Your code should just return the correct answer and optionally the key, but nothing else.
Try your code-cracker on this text, using the clue that the message starts with "It seems" (or "It_seems", if you use
the underscore):
I_rso_wotTe,taef_h__hl__socaeihtemonraaheamd_svemsp_l_ems_ayiN___Anofeadt.yueo_o
h_..__leaA_.iaastnY.snw__do__d_nyeuhl_foor_eiaotushlvrr.'oapee.avnv_d__he,ey_gOf
___oiunrbpaunieeer_r_l_geos_ctoingoloyfq_rcam__ilainpotlimadufhjv_llt_emiw_aevsd
nrsdriengieysr_p_tc_,tlfteuc_uitwrrawavzo_irhlez_ftrelszloyyry_bir__e_huv_no_ead
eauuyvsbs_mtoe_l.rb_urat_eeh_y_pOsreg_fjnp,rocucee___otn_cpgbmujltaayprgiayr_uep
fb_btt,velyahe_s,eogeraq__ue__ncysr.hcdzoo__ar_duftTcioi'tahkmnarwxeeeegeae_r__j

As you can see, there's plenty of punctuation in this text, but there are no new-lines, it is just one chunk of text.
And again, all spaces have been replaced with underscores for clarity, but you should remove those to make the
cleartext readable. If you do solve it, please put four spaces before the cleartext if you post it here, to hide it
for people who want to solve it themselves.

Thanks to SwimmingPastaDevil for suggesting this problem at /r/dailyprogrammer_ideas! If you have a problem that you
think would be a good challenge, why not head over there and help us out!
"""

import numpy as np
import random
import string

def matrix(inp, key):
    inp = inp.replace(' ', '_')
    length = len(inp)
    resh = np.reshape(list(inp), (int(length/key), key))
    trans = np.transpose(resh)
    return ''.join(list(np.reshape(trans, (1, length))[0])).replace('_', ' ')

def matrix_solver(cipher, clear):
    length = len(cipher)
    lol = []
    for c in clear:
        lol.append([i for i, ltr in enumerate(cipher) if ltr == c])
    done = False
    for i in lol[0]:
        for n, l in enumerate(lol):
            if i*(n+1) not in l:
                break
        else:
            return i
    else:
        print('Not found')

def main():
    ciphertext = ("I_rso_wotTe,taef_h__hl__socaeihtemonraaheamd_svemsp_l_ems_ayiN___Anofeadt.yueo_o"
                  "h_..__leaA_.iaastnY.snw__do__d_nyeuhl_foor_eiaotushlvrr.'oapee.avnv_d__he,ey_gOf"
                  "___oiunrbpaunieeer_r_l_geos_ctoingoloyfq_rcam__ilainpotlimadufhjv_llt_emiw_aevsd"
                  "nrsdriengieysr_p_tc_,tlfteuc_uitwrrawavzo_irhlez_ftrelszloyyry_bir__e_huv_no_ead"
                  "eauuyvsbs_mtoe_l.rb_urat_eeh_y_pOsreg_fjnp,rocucee___otn_cpgbmujltaayprgiayr_uep"
                  "fb_btt,velyahe_s,eogeraq__ue__ncysr.hcdzoo__ar_duftTcioi'tahkmnarwxeeeegeae_r__j")
    cleartext = 'It_seems'
    key = matrix_solver(ciphertext, cleartext[1:])
    decoded = matrix(ciphertext, key)
    print(decoded)

if __name__ == "__main__":
    main()


text:
package org.terracotta.voltron.proxy;

/**
 * @author Alex Snaps
 */
public interface Codec {

  byte[] encode(Class<?> type, Object value);

  byte[] encode(Class<?>[] types, Object[] values);

  Object decode(byte[] buffer, Class<?> type);

  Object[] decode(byte[] buffer, Class<?>[] type);
}


text: """Code for working with PDF files."""


text:
Stack Overflow Replica

Agreed on norms:

1) Styling will be discussed after functionality established

2) Regular Check-ins

3) Group stand up at the end of the day

4) @mention everyone on PRs

5) Use the Slack channel frequently

6) Call for help if stuck

7) Pull from master often

8) NEVER PUSH TO MASTER

9) Check Slack for new merges

10) Have fun!


text:
#ifndef TENSORFLOW_LITE_DELEGATES_GPU_CL_SELECTORS_FULLY_CONNECTED_SELECTOR_H_
#define TENSORFLOW_LITE_DELEGATES_GPU_CL_SELECTORS_FULLY_CONNECTED_SELECTOR_H_

#include <memory>

#include "tensorflow/lite/delegates/gpu/cl/kernels/gpu_operation.h"
#include "tensorflow/lite/delegates/gpu/common/operations.h"
#include "tensorflow/lite/delegates/gpu/common/status.h"

namespace tflite {
namespace gpu {
namespace cl {

absl::Status SelectFullyConnected(const FullyConnectedAttributes& attr,
                                  const CreationContext& creation_context,
                                  const OperationDef& op_def, int batch_size,
                                  std::unique_ptr<GPUOperation>* ptr);

}  // namespace cl
}  // namespace gpu
}  // namespace tflite

#endif  // TENSORFLOW_LITE_DELEGATES_GPU_CL_SELECTORS_FULLY_CONNECTED_SELECTOR_H_


text:
All notable changes to this add-on will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

## [30] - 2022-11-15
### Changed
- Dependency updates.
- Each imported endpoint is included in the selected context, unless its URL matches an already existing *Include in
  Context* regex entry.

## [29] - 2022-10-27
### Changed
- Update minimum ZAP version to 2.12.0.
- Remove parser used for core spider (Related to Issue 3113).
- Maintenance changes.

## [28] - 2022-09-21
### Added
- Imported specs are now persisted to the session database. They are used by the new variant to mark path parameters as
  Data Driven Nodes.

### Fixed
- JSON body examples specified under `schema` were being enclosed in quotes.
- Error message when `apiFile` field is not accessible was outputting the `targetUrl` and not the incorrect filename (Issue 7370).

### Changed
- Maintenance changes.
- Use Spider add-on (Issue 3113).
- Use Form Handler add-on directly.
- DDNs added as Structural Modifiers have been superseded by a custom variant. The variant supports nested DDNs and leaf
  DDNs, prevents non-parameter URL paths from being merged with DDNs, and treats paths with different HTTP methods
  uniquely. DDNs are named with the parameter name from the spec.

## [27] - 2022-03-29
### Added
- Support content field (JSON) in parameters (Issue 6166).

### Changed
- Now depends on commonlib for display of import progress (Issue 6783).
- Dependency updates.

### Fixed
- Properly generate Content-Type header when in presence of more than one supported content (Issue 7082).
- Quote provided string values in JSON content (Issue 7128).
- Properly handle empty default values in server variables.

## [26] - 2022-02-01

### Fixed
- Do not report "Unrecognised parameter" for valid parameters.

## [25] - 2022-01-18
### Changed
- Update minimum ZAP version to 2.11.1.
- Dependency updates.
- When the automation Job is edited via UI Dialog then the status will be set to Not started

### Fixed
- Parameter examples specified as part of the schema were not being used.

## [24] - 2021-12-06
### Changed
- Use examples defined in parameters ([Issue #6870](https://github.com/zaproxy/zaproxy/issues/6870)).
- Tweak error message shown when content type is not supported.
- Dependency updates.

### Fixed
- Fixed ClassCastException when using nested map properties with mixed definition styles.

## [23] - 2021-10-06
### Fixed
- Fixed StackOverflow in the Body/DataGenerator when an invalid property type is specified. ([Issue #6591](https://github.com/zaproxy/zaproxy/issues/6591))

### Added
- Use path and operation servers ([Issue #6754](https://github.com/zaproxy/zaproxy/issues/6754)).

### Changed
- Warn when request has content type `application/xml`, not supported (Related to [Issue #6767](https://github.com/zaproxy/zaproxy/issues/6767)).
- Maintenance changes.
- Update minimum ZAP version to 2.11.0.

## [22] - 2021-09-16
### Changed
- Maintenance changes.

## [21] - 2021-09-01
### Added
- The import progress is now displayed using a Progress Panel.

### Fixed
- Fixed var support in URLs ([Issue #6726](https://github.com/zaproxy/zaproxy/issues/6726))
- Import file definition even if it has issues ([Issue #6758](https://github.com/zaproxy/zaproxy/issues/6758)).

### Changed
- Use `application/json` media type examples when available.

## [20] - 2021-08-05
### Added
- Automation Framework GUI

### Changed
- Maintenance changes.

### Fixed
- Fix RequestMethod enum name for OPTIONS (Issue 6666)

## [19] - 2021-06-29
### Added
- Added support for Multipart form-data (Issue 6418).

### Changed
- Always use enum values when defined (Issue 6489).
- Now using 2.10 logging infrastructure (Log4j 2.x).
- Automation parameters are now in camelCase. This is a breaking change, and older automation configurations containing all-lowercase openapi parameters will stop working.
- The import dialogs now show the values used in the previous import when reopened.
- Maintenance changes.

### Fixed
- NPE if form has no schema element.

## [18] - 2021-03-09
### Added
- Support for the Automation Framework
- Support for statistics (number of URLs added)

### Changed
- Maintenance changes.

## [17] - 2020-12-15
### Added
- Handle cookie parameters (Issue 6045).
- Use default values in `x-www-form-urlencoded` and `json` bodies (Issue 6095).

### Changed
- Show import exceptions in the Output tab (Issue 6042).
- Maintenance changes.
- Update minimum ZAP version to 2.10.0.

### Fixed
- Add imported messages synchronously to the Sites tree (Issue 5936).
- Correct parent dialogue when choosing the file to import (Issue 6041).
- Properly handle no schema when generating the request body (Issue 6042).
- Return API error `illegal_parameter` (instead of `internal_error`) when unable to get the OpenAPI definition from the provided URL.

## [16] - 2020-06-09
### Added
- Map Structure support for OpenAPI v3.0 (Issue 5863).
- Using OpenAPI Example values for value generation in request bodies and urls (Issue 5168).

### Changed
- Improve content checks when spidering for specifications (Issue 5725).
- Update minimum ZAP version to 2.9.0.
- Maintenance changes.

### Fixed
- Notify all redirects followed for proper passive scanning.

## [15] - 2020-01-17
### Added
- Add info and repo URLs.

### Changed
- Promote addon to Beta.

## [14] - 2019-12-02
### Added
- Support OpenAPI v3.0 (Issue 4549).
- Allow to specify the target URL (scheme, authority, and path) when importing through the command line.

### Changed
- Do not consume spider resource if not parsed as OpenAPI definition.
- Allow to specify the target URL when importing from file through the API and GUI.
- Allow to override also the scheme and path when importing from URL through the API.

## [13] - 2019-07-18

- Added Accept header for importing an OpenAPI definition from an URL, in the proper format.
- Correct import of v1.2 definitions (Issue 5262).
- Fix exception when reporting errors.
- Update minimum ZAP version to 2.8.0.
- Add import menu to (new) top level Import menu instead of Tools menu.
- Add support for primitive values (standalone and within arrays) in a request body (Issue 5250).

## 12 - 2018-05-18

- Ignore BOM when parsing and don't rely on default character encoding (Issue 4676).

## 11 - 2018-05-15

- Include exception message in warning dialog when a parse error occurs (Issue 4667).
- Open previously chosen directory when importing local file.

## 10 - 2018-01-17

- Fallback to host of request URI (Issue 4271).

## 9 - 2017-12-13

- Update Swagger/OpenAPI parser (Issue 3479).
- Fix exception with ref parameters.

## 8 - 2017-11-24

- Fix NPE in BodyGenerator.
- Fix NPEs when a parameter is null.

## 7 - 2017-09-28

- Correct validations when importing a file through the API.

## 6 - 2017-06-02

- Support optional host override.
- Detect and warn on potential loops.
- Allow add-on to be unloaded dynamically.
- Support user specified values when importing (Issue 3344).
- Support older swagger formats (Issue 3598).

## 5 - 2017-05-05

- Run synchronously and return any warnings when importing via API or cmdline.

## 4 - 2017-04-21

- Fallback to scheme of request URI (Issue 3433).

## 3 - 2017-04-20

- Added cmdline support.

## 2 - 2017-04-18

- Configure Swagger library logging.

## 1 - 2017-03-30

- First Version

[30]: https://github.com/zaproxy/zap-extensions/releases/openapi-v30
[29]: https://github.com/zaproxy/zap-extensions/releases/openapi-v29
[28]: https://github.com/zaproxy/zap-extensions/releases/openapi-v28
[27]: https://github.com/zaproxy/zap-extensions/releases/openapi-v27
[26]: https://github.com/zaproxy/zap-extensions/releases/openapi-v26
[25]: https://github.com/zaproxy/zap-extensions/releases/openapi-v25
[24]: https://github.com/zaproxy/zap-extensions/releases/openapi-v24
[23]: https://github.com/zaproxy/zap-extensions/releases/openapi-v23
[22]: https://github.com/zaproxy/zap-extensions/releases/openapi-v22
[21]: https://github.com/zaproxy/zap-extensions/releases/openapi-v21
[20]: https://github.com/zaproxy/zap-extensions/releases/openapi-v20
[19]: https://github.com/zaproxy/zap-extensions/releases/openapi-v19
[18]: https://github.com/zaproxy/zap-extensions/releases/openapi-v18
[17]: https://github.com/zaproxy/zap-extensions/releases/openapi-v17
[16]: https://github.com/zaproxy/zap-extensions/releases/openapi-v16
[15]: https://github.com/zaproxy/zap-extensions/releases/openapi-v15
[14]: https://github.com/zaproxy/zap-extensions/releases/openapi-v14
[13]: https://github.com/zaproxy/zap-extensions/releases/openapi-v13


text:
<?php
    $args = $params;
    $label = !empty($args->fields['fields'][$args->field]['label']) ? $args->fields['fields'][$args->field]['label'] : '';
    $class = !empty($args->fields['fields'][$args->field]['class']) ? $args->fields['fields'][$args->field]['class'] : '';
    $note = !empty($args->fields['fields'][$args->field]['note']) ? $args->fields['fields'][$args->field]['note'] : '';
    $atts = !empty($args->fields['fields'][$args->field]['atts']) ? $args->fields['fields'][$args->field]['atts'] : '';
    if (is_array($atts)) {
        $atts_s = '';
        foreach ($atts as $key => $value) {
            $atts_s .= $key . '="' . $value . '"';
        }
        $atts = $atts_s;
    }

    $value = esc_attr($args->data[$args->field]);
?>

    <?php echo $label;?>

            <input <?php echo $atts;?> class="<?php echo !empty($class) ? $class : '';?> agp-color-picker" type="text" id="<?php echo "{$args->key}[{$args->field}]"; ?>" name="<?php echo "{$args->key}[{$args->field}]"; ?>" value="<?php echo $value;?>">
            <?php if (!empty($note)): ?>

<?php echo $note;?>

            <?php endif;?>


text:
package messagebus

import (
	"github.com/stretchr/testify/mock"
)

type MessageBusMock struct {
	mock.Mock
}

func (m *MessageBusMock) Publish(v Message) {
	m.Called(v)
}

type DevNullBus struct{}

func (m *DevNullBus) Publish(v Message) {}


text:
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cxf="http://camel.apache.org/schema/cxf"

       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
    ">

   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>

   <!-- START SNIPPET: example -->
   <cxf:cxfEndpoint id="routerEndpoint" address="http://localhost:${CXFTestSupport.port2}/CXFGreeterEnrichTest/CamelContext/RouterPort"
    		serviceClass="org.apache.hello_world_soap_http.GreeterImpl"
    		skipFaultLogging="true">
        <cxf:outInterceptors>
            <!-- This interceptor will force the CXF server send the XML start document to client -->
            <bean class="org.apache.camel.component.cxf.WriteXmlDeclarationInterceptor"/>
        </cxf:outInterceptors>
        <cxf:properties>
            <!-- Set the publishedEndpointUrl which could override the service address from generated WSDL as you want -->
    	    <entry key="publishedEndpointUrl" value="http://www.simple.com/services/test" />
    	</cxf:properties>
   </cxf:cxfEndpoint>
   <!-- END SNIPPET: example -->

   <cxf:cxfEndpoint id="serviceEndpoint" address="http://localhost:${CXFTestSupport.port1}/CXFGreeterEnrichTest/SoapContext/SoapPort"
    		wsdlURL="testutils/hello_world.wsdl"
    		serviceClass="org.apache.hello_world_soap_http.Greeter"
    		endpointName="s:SoapPort"
    		serviceName="s:SOAPService"
    	xmlns:s="http://apache.org/hello_world_soap_http" />

   <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route errorHandlerRef="noErrorHandler">
      <from ref="routerEndpoint" />
      <enrich ref="serviceEndpoint" />
    </route>
   </camelContext>

   <bean id="noErrorHandler" class="org.apache.camel.builder.NoErrorHandlerBuilder"/>

</beans>


text:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<context:annotation-config/>
	<context:property-placeholder/>

	<bean id="helloService" class="sample.xml.service.HelloWorldService"/>
	<bean id="application" class="sample.xml.SampleSpringXmlApplication"/>

</beans>


text: Project is developed in python to pull data from a website and try to analyse it.


text:
from kfp import compiler
from kfp import dsl

@dsl.component
def add(a: float, b: float) -> float:
    return a + b

@dsl.pipeline(name='test-pipeline')
def my_pipeline(a: float = 1, b: float = 7):
    add_task = add(a=a, b=b)
    add_task.set_retry(num_retries=3)

if __name__ == '__main__':
    compiler.Compiler().compile(
        pipeline_func=my_pipeline,
        package_path=__file__.replace('.py', '.yaml'))