Skip to content
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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion rust_src/src/character.rs
@@ -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};

Expand Down Expand Up @@ -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))
}
}
1 change: 1 addition & 0 deletions rust_src/src/lib.rs
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions rust_src/src/multibyte.rs
Expand Up @@ -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 {
Expand Down
23 changes: 0 additions & 23 deletions src/character.c
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down