New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port multibyte-char-to-unibyte #236

Merged
merged 1 commit into from Jul 23, 2017
Jump to file or symbol
Failed to load files and symbols.
+30 −24
Diff settings

Always

Just for now

Copy path View file
@@ -1,7 +1,7 @@
//! Operations on characters.
use lisp::LispObject;
use multibyte::{MAX_CHAR, make_char_multibyte};
use multibyte::{MAX_CHAR, make_char_multibyte, raw_byte_from_codepoint_safe};
use remacs_macros::lisp_fn;
use remacs_sys::{EmacsInt, error};
@@ -38,3 +38,17 @@ fn unibyte_char_to_multibyte(ch: LispObject) -> LispObject {
}
LispObject::from_fixnum(make_char_multibyte(c) as EmacsInt)
}
/// Convert the multibyte character CH to a byte.
/// If the multibyte character does not represent a byte, return -1.
#[lisp_fn]
fn multibyte_char_to_unibyte(ch: LispObject) -> LispObject {
let c = ch.as_character_or_error();
if c < 256 {
// Can't distinguish a byte read from a unibyte buffer from
// a latin1 char, so let's let it slide.
ch
} else {
LispObject::from_fixnum(raw_byte_from_codepoint_safe(c))
}
}
Copy path View file
@@ -202,6 +202,7 @@ pub extern "C" fn rust_init_syms() {
defsubr(&*character::Scharacterp);
defsubr(&*character::Schar_or_string_p);
defsubr(&*character::Sunibyte_char_to_multibyte);
defsubr(&*character::Smultibyte_char_to_unibyte);
defsubr(&*vectors::Sarrayp);
defsubr(&*vectors::Sbool_vector_p);
defsubr(&*vectors::Sbufferp);
Copy path View file
@@ -191,6 +191,20 @@ fn raw_byte_from_codepoint(cp: Codepoint) -> c_uchar {
(cp - 0x3F_FF00) as c_uchar
}
/// Same as the CHAR_TO_BYTE_SAFE macro.
/// Return the raw 8-bit byte for character CP,
/// or -1 if CP doesn't correspond to a byte.
#[inline]
pub fn raw_byte_from_codepoint_safe(cp: Codepoint) -> EmacsInt {
if cp < 0x80 {
cp as EmacsInt
} else if cp > MAX_5_BYTE_CHAR {
raw_byte_from_codepoint(cp) as EmacsInt
} else {
-1
}
}
/// UNIBYTE_TO_CHAR macro
#[inline]
pub fn unibyte_to_char(cp: Codepoint) -> Codepoint {
Copy path View file
@@ -67,28 +67,6 @@ translate_char (Lisp_Object table, int c)
return c;
}
DEFUN ("multibyte-char-to-unibyte", Fmultibyte_char_to_unibyte,
Smultibyte_char_to_unibyte, 1, 1, 0,
doc: /* Convert the multibyte character CH to a byte.
If the multibyte character does not represent a byte, return -1. */)
(Lisp_Object ch)
{
int cm;
CHECK_CHARACTER (ch);
cm = XFASTINT (ch);
if (cm < 256)
/* Can't distinguish a byte read from a unibyte buffer from
a latin1 char, so let's let it slide. */
return ch;
else
{
int cu = CHAR_TO_BYTE_SAFE (cm);
return make_number (cu);
}
}
/* Return width (columns) of C considering the buffer display table DP. */
static ptrdiff_t
@@ -632,7 +610,6 @@ syms_of_character (void)
staticpro (&Vchar_unify_table);
Vchar_unify_table = Qnil;
defsubr (&Smultibyte_char_to_unibyte);
defsubr (&Schar_width);
defsubr (&Sstring_width);
defsubr (&Sstring);
ProTip! Use n and p to navigate between commits in a pull request.