<commit_before>// Copyright 2018, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use libc;
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::ffi::{CStr, CString, OsStr};
use std::fmt;
use std::hash;
use std::ops::Deref;
use std::os::raw::c_char;
use std::ptr;
use std::slice;
use std::string::String;
use translate::*;
use types::{StaticType, Type};

use glib_sys;
use gobject_sys;
use value::{FromValueOptional, SetValue, SetValueOptional, Value};

#[derive(Debug)]
pub struct GString(Inner);

#[derive(Debug)]
enum Inner {
    Native(Option<CString>),
    Foreign(*mut c_char, usize),
}

unsafe impl Send for GString {}
unsafe impl Sync for GString {}

impl GString {
    pub unsafe fn new(ptr: *mut c_char) -> Self {
        assert!(!ptr.is_null());
        GString(Inner::Foreign(ptr, libc::strlen(ptr)))
    }

    pub unsafe fn new_borrowed(ptr: *const c_char) -> Borrowed<Self> {
        assert!(!ptr.is_null());
        Borrowed::new(GString(Inner::Foreign(ptr as *mut _, libc::strlen(ptr))))
    }

    pub fn as_str(&self) -> &str {
        let cstr = match self {
            GString(Inner::Foreign(ptr, length)) => unsafe {
                let bytes = slice::from_raw_parts(*ptr as *const u8, length + 1);
                CStr::from_bytes_with_nul_unchecked(bytes)<commit_msg>clippy: Document GString::new{,_borrowed} for safety<commit_after>// Copyright 2018, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use libc;
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::ffi::{CStr, CString, OsStr};
use std::fmt;
use std::hash;
use std::ops::Deref;
use std::os::raw::c_char;
use std::ptr;
use std::slice;
use std::string::String;
use translate::*;
use types::{StaticType, Type};

use glib_sys;
use gobject_sys;
use value::{FromValueOptional, SetValue, SetValueOptional, Value};

#[derive(Debug)]
pub struct GString(Inner);

#[derive(Debug)]
enum Inner {
    Native(Option<CString>),
    Foreign(*mut c_char, usize),
}

unsafe impl Send for GString {}
unsafe impl Sync for GString {}

impl GString {
    /// Create a new GString from a glib-originated string, taking ownership.
    ///
    /// # Safety
    ///
    /// The provided string must be a valid C string (i.e. `'0'` terminated).
    ///
    /// The provided pointer must be allocated by glib such that `g_free(ptr)`
    /// is valid, as the `GString` will call `g_free(ptr)` on drop.
    ///
    /// It is essential that noone else free this pointer as it owned by the
    /// returned `GString`.
    ///
    /// The underlying string must not be mutated, in particular in terms of
    /// length, underneath the `GString` instance.
    pub unsafe fn new(ptr: *mut c_char) -> Self {
        assert!(!ptr.is_null());
        GString(Inner::Foreign(ptr, libc::strlen(ptr)))
    }

    /// Create a new GString from a glib-originated string, borrowing it rather
    /// than taking ownership.
    ///
    /// # Safety
    ///
    /// The provided string must be a valid C string (i.e. `'0'` terminated).
    ///
    /// It is essential that noone else free this pointer as it owned by the
    /// returned `GString` until the borrow is dropped.
    ///
    /// The underlying string must not be mutated, in particular in terms of
    /// length, underneath the `GString` instance for the duration of the borrow.
    pub unsafe fn new_borrowed(ptr: *const c_char) -> Borrowed<Self> {
        assert!(!ptr.is_null());
        Borrowed::new(GString(Inner::Foreign(ptr as *mut _, libc::strlen(ptr))))
    }

    pub fn as_str(&self) -> &str {
        let cstr = match self {
            GString(Inner::Foreign(ptr, length)) => unsafe {
                let bytes = slice::from_raw_parts(*ptr as *const u8, length + 1);
                CStr::from_bytes_with_nul_unchecked(bytes)
{'fraction_non_alphanumeric': 0.14018691588785046, 'fraction_numerical': 0.003443187407771766, 'mean_word_length': 3.2188796680497926, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3896531', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1279, 'n_words': 415}