<commit_before>// Copyright 2013 The rust-pcre authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[link(name = "pcre", package_id = "pcre", vers = "0.1")];
#[crate_type = "lib"];

extern mod extra;

use extra::enum_set::{CLike, EnumSet};
use extra::treemap::{TreeMap};
use std::c_str;
use std::libc::{c_char, c_int, c_uchar, c_void};
use std::option::{Option};
use std::ptr;
use std::result::{Result};
use std::vec;

mod detail;

pub type options = c_int;

pub static PCRE_CASELESS: options = 0x00000001;
pub static PCRE_MULTILINE: options = 0x00000002;
pub static PCRE_DOTALL: options = 0x00000004;
pub static PCRE_EXTENDED: options = 0x00000008;
pub static PCRE_ANCHORED: options = 0x00000010;
pub static PCRE_DOLLAR_ENDONLY: options = 0x00000020;
pub static PCRE_EXTRA: options = 0x00000040;
pub static PCRE_NOTBOL: options = 0x00000080;
pub static PCRE_NOTEOL: options = 0x00000100;
pub static PCRE_UNGREEDY: options = 0x00000200;
pub static PCRE_NOTEMPTY: options = 0x00000400;
pub static PCRE_UTF8: options = 0x00000800;
pub static PCRE_NO_AUTO_CAPTURE: options = 0x00001000;
pub static PCRE_NO_UTF8_CHECK: options = 0x00002000;
pub static PCRE_AUTO_CALLOUT: options = 0x00004000;
pub static PCRE_PARTIAL_SOFT: options = 0x00008000;
pub static PCRE_PARTIAL: options = 0x00008000;
pub static PCRE_NEVER_UTF: options = 0x00010000;
pub static PCRE_DFA_SHORTEST: options = 0x00010000;
pub static PCRE_DFA_RESTART: options = 0x00020000;
pub static PCRE_FIRSTLINE: options = 0x00040000;
pub static PCRE_DUPNAMES: options = 0x00080000;
pub static PCRE_NEWLINE_CR: options = 0x00100000;
pub static PCRE_NEWLINE_LF: options = 0x00200000;
pub static PCRE_NEWLINE_CRLF: options = 0x00300000;
pub static PCRE_NEWLINE_ANY: options = 0x00400000;
pub static PCRE_NEWLINE_ANYCRLF: options = 0x00500000;
pub static PCRE_BSR_ANYCRLF: options = 0x00800000;
pub static PCRE_BSR_UNICODE: options = 0x01000000;
pub static PCRE_JAVASCRIPT_COMPAT: options = 0x02000000;
pub static PCRE_NO_START_OPTIMIZE: options = 0x04000000;
pub static PCRE_NO_START_OPTIMISE: options = 0x04000000;
pub static PCRE_PARTIAL_HARD: options = 0x08000000;
pub static PCRE_NOTEMPTY_ATSTART: options = 0x10000000;
pub static PCRE_UCP: options = 0x20000000;

pub enum StudyOption {
    JitCompile = 0x0001,
    JitPartialSoftCompile = 0x0002,
    JitPartialHardCompile = 0x0004,
    ExtraNeeded = 0x0008
}

pub struct CompilationError {

    priv opt_err: Option<~str>,

    priv erroffset: c_int

}

/// Wrapper for libpcre's `pcre` object (representing a compiled regular expression).
pub struct Pcre {

    priv code: *detail::pcre,

    priv extra: *detail::pcre_extra,

    priv capture_count_: c_int

}

/// Represents a match of a subject string against a regular expression.
pub struct Match<'self> {

    priv subject: &'self str,

    priv partial_ovector: ~[c_int],

    priv string_count_: c_int

}

/// Iterator type for iterating matches within a subject string.
pub struct MatchIterator<'self> {

    priv code: *detail::pcre,

    priv extra: *detail::pcre_extra,

    priv capture_count: c_int,

    priv subject: &'self str,

    /// The subject string as a `CString`. In MatchIterator's next() method, this is re-used
    /// each time so that only one C-string copy of the subject string needs to be allocated.
    priv subject_cstring: c_str::CString,

    priv offset: c_int,

    priv options: options,

    priv ovector: ~[c_int]

}

impl CLike for StudyOption {
    fn from_uint(v: uint) -> StudyOption {
        match v {
            0x0001 => JitCompile,
            0x0002 => JitPartialSoftCompile,
            0x0004 => JitPartialHardCompile,
            0x0008 => ExtraNeeded,
            _ => fail!("unknown StudyOption value {:u}", v)
        }
    }

    fn to_uint(&self) -> uint {
        *self as uint
    }
}

impl CompilationError {
    pub fn message(&self) -> Option<~str> {
        self.opt_err.clone()
    }

    pub fn offset(&self) -> uint {
        self.erroffset as uint
    }
}

impl ToStr for CompilationError {
    fn to_str(&self) -> ~str {
        match self.opt_err {
            None => format!("compilation failed at offset {:u}", self.erroffset as uint),<commit_msg>Fix the CLike impl for StudyOption<commit_after>// Copyright 2013 The rust-pcre authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[link(name = "pcre", package_id = "pcre", vers = "0.1")];
#[crate_type = "lib"];

extern mod extra;

use extra::enum_set::{CLike, EnumSet};
use extra::treemap::{TreeMap};
use std::c_str;
use std::libc::{c_char, c_int, c_uchar, c_void};
use std::option::{Option};
use std::ptr;
use std::result::{Result};
use std::vec;

mod detail;

pub type options = c_int;

pub static PCRE_CASELESS: options = 0x00000001;
pub static PCRE_MULTILINE: options = 0x00000002;
pub static PCRE_DOTALL: options = 0x00000004;
pub static PCRE_EXTENDED: options = 0x00000008;
pub static PCRE_ANCHORED: options = 0x00000010;
pub static PCRE_DOLLAR_ENDONLY: options = 0x00000020;
pub static PCRE_EXTRA: options = 0x00000040;
pub static PCRE_NOTBOL: options = 0x00000080;
pub static PCRE_NOTEOL: options = 0x00000100;
pub static PCRE_UNGREEDY: options = 0x00000200;
pub static PCRE_NOTEMPTY: options = 0x00000400;
pub static PCRE_UTF8: options = 0x00000800;
pub static PCRE_NO_AUTO_CAPTURE: options = 0x00001000;
pub static PCRE_NO_UTF8_CHECK: options = 0x00002000;
pub static PCRE_AUTO_CALLOUT: options = 0x00004000;
pub static PCRE_PARTIAL_SOFT: options = 0x00008000;
pub static PCRE_PARTIAL: options = 0x00008000;
pub static PCRE_NEVER_UTF: options = 0x00010000;
pub static PCRE_DFA_SHORTEST: options = 0x00010000;
pub static PCRE_DFA_RESTART: options = 0x00020000;
pub static PCRE_FIRSTLINE: options = 0x00040000;
pub static PCRE_DUPNAMES: options = 0x00080000;
pub static PCRE_NEWLINE_CR: options = 0x00100000;
pub static PCRE_NEWLINE_LF: options = 0x00200000;
pub static PCRE_NEWLINE_CRLF: options = 0x00300000;
pub static PCRE_NEWLINE_ANY: options = 0x00400000;
pub static PCRE_NEWLINE_ANYCRLF: options = 0x00500000;
pub static PCRE_BSR_ANYCRLF: options = 0x00800000;
pub static PCRE_BSR_UNICODE: options = 0x01000000;
pub static PCRE_JAVASCRIPT_COMPAT: options = 0x02000000;
pub static PCRE_NO_START_OPTIMIZE: options = 0x04000000;
pub static PCRE_NO_START_OPTIMISE: options = 0x04000000;
pub static PCRE_PARTIAL_HARD: options = 0x08000000;
pub static PCRE_NOTEMPTY_ATSTART: options = 0x10000000;
pub static PCRE_UCP: options = 0x20000000;

pub enum StudyOption {
    JitCompile = 0x0001,
    JitPartialSoftCompile = 0x0002,
    JitPartialHardCompile = 0x0004,
    ExtraNeeded = 0x0008
}

pub struct CompilationError {

    priv opt_err: Option<~str>,

    priv erroffset: c_int

}

/// Wrapper for libpcre's `pcre` object (representing a compiled regular expression).
pub struct Pcre {

    priv code: *detail::pcre,

    priv extra: *detail::pcre_extra,

    priv capture_count_: c_int

}

/// Represents a match of a subject string against a regular expression.
pub struct Match<'self> {

    priv subject: &'self str,

    priv partial_ovector: ~[c_int],

    priv string_count_: c_int

}

/// Iterator type for iterating matches within a subject string.
pub struct MatchIterator<'self> {

    priv code: *detail::pcre,

    priv extra: *detail::pcre_extra,

    priv capture_count: c_int,

    priv subject: &'self str,

    /// The subject string as a `CString`. In MatchIterator's next() method, this is re-used
    /// each time so that only one C-string copy of the subject string needs to be allocated.
    priv subject_cstring: c_str::CString,

    priv offset: c_int,

    priv options: options,

    priv ovector: ~[c_int]

}

impl CLike for StudyOption {
    fn from_uint(n: uint) -> StudyOption {
        match n {
            1u => JitCompile,
            2u => JitPartialSoftCompile,
            3u => JitPartialHardCompile,
            4u => ExtraNeeded,
            _ => fail!("unknown StudyOption number {:u}", n)
        }
    }

    fn to_uint(&self) -> uint {
        match *self {
            JitCompile => 1u,
            JitPartialSoftCompile => 2u,
            JitPartialHardCompile => 3u,
            ExtraNeeded => 4u
        }
    }
}

impl CompilationError {
    pub fn message(&self) -> Option<~str> {
        self.opt_err.clone()
    }

    pub fn offset(&self) -> uint {
        self.erroffset as uint
    }
}

impl ToStr for CompilationError {
    fn to_str(&self) -> ~str {
        match self.opt_err {
            None => format!("compilation failed at offset {:u}", self.erroffset as uint),
{'fraction_non_alphanumeric': 0.09630694383016364, 'fraction_numerical': 0.07983193277310924, 'mean_word_length': 3.902439024390244, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, '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': '3463002', 'n_tokens_mistral': 3683, 'n_tokens_neox': 3067, 'n_words': 901}