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

Replace clip_to_bounds with num::clamp #1570

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
83 changes: 76 additions & 7 deletions rust_src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust_src/Cargo.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ lazy_static = "1.2"
libc = "0.2"
line-wrap = "0.1.1"
md5 = "0.6"
num = "0.2"
rand = "0.6.5"
sha1 = "0.6"
sha2 = "0.8"
Expand Down
8 changes: 6 additions & 2 deletions rust_src/src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{self, iter, mem, ops, ptr, slice};

use field_offset::FieldOffset;
use libc::{self, c_char, c_uchar, c_void, ptrdiff_t};
use num;

use rand::{rngs::OsRng, Rng};

Expand Down Expand Up @@ -59,7 +60,6 @@ use crate::{
strings::string_equal,
textprop::get_text_property,
threads::{c_specpdl_index, ThreadState},
util::clip_to_bounds,
vectors::LispVectorlikeRef,
};

Expand Down Expand Up @@ -1412,7 +1412,11 @@ fn get_truename_buffer_1(filename: LispSymbolOrString) -> LispObject {
/// for positions far away from POS).
#[lisp_fn]
pub fn overlay_recenter(pos: LispNumber) {
let p = clip_to_bounds(isize::min_value(), pos.to_fixnum(), isize::max_value());
let p = num::clamp(
pos.to_fixnum() as isize,
isize::min_value(),
isize::max_value(),
);
unsafe {
recenter_overlay_lists(ThreadState::current_buffer_unchecked().as_mut(), p);
}
Expand Down
12 changes: 8 additions & 4 deletions rust_src/src/editfns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::ptr;

use libc;
use libc::{c_char, c_int, c_uchar, ptrdiff_t};
use num;

use remacs_macros::lisp_fn;

Expand Down Expand Up @@ -43,7 +44,6 @@ use crate::{
textprop::get_char_property,
threads::{c_specpdl_index, ThreadState},
time::{lisp_time_struct, time_overflow, LispTime},
util::clip_to_bounds,
windows::{selected_window, LispWindowRef},
};

Expand Down Expand Up @@ -136,7 +136,7 @@ fn region_limit(beginningp: bool) -> EmacsInt {
if ((current_buf.pt as EmacsInt) < num) == beginningp {
current_buf.pt as EmacsInt
} else {
clip_to_bounds(current_buf.begv, num, current_buf.zv) as EmacsInt
num::clamp(num as isize, current_buf.begv, current_buf.zv) as EmacsInt
}
}

Expand Down Expand Up @@ -186,7 +186,7 @@ pub fn goto_char(position: LispObject) -> LispObject {
set_point_from_marker(position);
} else if let Some(num) = position.as_fixnum() {
let cur_buf = ThreadState::current_buffer_unchecked();
let pos = clip_to_bounds(cur_buf.begv, num, cur_buf.zv);
let pos = num::clamp(num as isize, cur_buf.begv, cur_buf.zv);
let bytepos = cur_buf.charpos_to_bytepos(pos);
unsafe { set_point_both(pos, bytepos) };
} else {
Expand Down Expand Up @@ -522,7 +522,11 @@ pub fn line_end_position(n: Option<EmacsInt>) -> EmacsInt {

let n = n.unwrap_or(1);

let clipped_n = clip_to_bounds(ptrdiff_t::min_value() + 1, n, ptrdiff_t::max_value());
let clipped_n = num::clamp(
n as isize,
ptrdiff_t::min_value() + 1,
ptrdiff_t::max_value(),
);
let end_pos = unsafe {
find_before_next_newline(
orig as isize,
Expand Down
5 changes: 4 additions & 1 deletion rust_src/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern crate lazy_static;
extern crate base64 as base64_crate;
extern crate libc;
extern crate md5;
extern crate num;
extern crate rand;
extern crate sha1;
extern crate sha2;
Expand Down Expand Up @@ -123,7 +124,6 @@ mod terminal;
mod textprop;
mod threads;
mod time;
mod util;
mod vectors;
mod window_configuration;
mod windows;
Expand All @@ -133,6 +133,9 @@ mod xml;
#[cfg(feature = "window-system-x11")]
mod xsettings;

// C exports.
mod utils_ffi;

#[cfg(all(not(test), target_os = "macos", feature = "unexecmacosx"))]
use alloc_unexecmacosx::OsxUnexecAlloc;

Expand Down
28 changes: 12 additions & 16 deletions rust_src/src/marker.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! marker support

use libc::{c_void, ptrdiff_t};
use std::ptr;

use libc::{c_void, ptrdiff_t};
use num;

use remacs_macros::lisp_fn;

use crate::{
Expand All @@ -13,7 +15,6 @@ use crate::{
remacs_sys::{equal_kind, EmacsInt, Lisp_Buffer, Lisp_Marker, Lisp_Misc_Type, Lisp_Type},
remacs_sys::{Qinteger_or_marker_p, Qmarkerp},
threads::ThreadState,
util::clip_to_bounds,
};

pub type LispMarkerRef = ExternalPtr<Lisp_Marker>;
Expand Down Expand Up @@ -254,20 +255,16 @@ pub fn point_max_marker() -> LispMarkerRef {
pub extern "C" fn set_point_from_marker(marker: LispObject) {
let marker: LispMarkerRef = marker.into();
let cur_buf = ThreadState::current_buffer_unchecked();
let charpos = clip_to_bounds(
cur_buf.begv,
marker.charpos_or_error() as EmacsInt,
cur_buf.zv,
);
let charpos = num::clamp(marker.charpos_or_error(), cur_buf.begv, cur_buf.zv);

// Don't trust the byte position if the marker belongs to a
// different buffer.
let bytepos = if marker.buffer().map_or(false, |b| b != cur_buf) {
cur_buf.charpos_to_bytepos(charpos)
} else {
clip_to_bounds(
num::clamp(
marker.bytepos_or_error(),
cur_buf.begv_byte,
marker.bytepos_or_error() as EmacsInt,
cur_buf.zv_byte,
)
};
Expand Down Expand Up @@ -330,7 +327,7 @@ pub fn copy_marker(marker: LispObject, itype: LispObject) -> LispObject {
#[lisp_fn]
pub fn buffer_has_markers_at(position: EmacsInt) -> bool {
let cur_buf = ThreadState::current_buffer_unchecked();
let position = clip_to_bounds(cur_buf.begv, position, cur_buf.zv);
let position = num::clamp(position as isize, cur_buf.begv, cur_buf.zv);

cur_buf.markers().map_or(false, |marker| {
marker
Expand Down Expand Up @@ -471,9 +468,8 @@ pub extern "C" fn set_marker_restricted_both(
.or_else(|| current_buffer().as_live_buffer())
{
let cur_buf = ThreadState::current_buffer_unchecked();
let clipped_charpos = clip_to_bounds(cur_buf.begv, charpos as EmacsInt, cur_buf.zv);
let clipped_bytepos =
clip_to_bounds(cur_buf.begv_byte, bytepos as EmacsInt, cur_buf.zv_byte);
let clipped_charpos = num::clamp(charpos, cur_buf.begv, cur_buf.zv);
let clipped_bytepos = num::clamp(bytepos, cur_buf.begv_byte, cur_buf.zv_byte);
attach_marker(m.as_mut(), b.as_mut(), clipped_charpos, clipped_bytepos);
} else {
unchain_marker(m.as_mut());
Expand Down Expand Up @@ -555,7 +551,7 @@ fn set_marker_internal_else(
}
let beg = buf.buffer_beg(restricted);
let end = buf.buffer_end(restricted);
charpos = clip_to_bounds(beg, charpos as EmacsInt, end);
charpos = num::clamp(charpos, beg, end);

// Don't believe BYTEPOS if it comes from a different buffer,
// since that buffer might have a very different correspondence
Expand All @@ -567,9 +563,9 @@ fn set_marker_internal_else(
{
buf.charpos_to_bytepos(charpos)
} else {
clip_to_bounds(
num::clamp(
bytepos,
buf.buffer_beg_byte(restricted),
bytepos as EmacsInt,
buf.buffer_end_byte(restricted),
)
};
Expand Down
15 changes: 0 additions & 15 deletions rust_src/src/util.rs

This file was deleted.

8 changes: 4 additions & 4 deletions rust_src/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{cmp, convert::TryFrom, fmt, ptr};

use libc::c_int;
use num;

use remacs_macros::lisp_fn;

Expand Down Expand Up @@ -36,7 +37,6 @@ use crate::{
Qwindow_live_p, Qwindow_valid_p, Qwindowp,
},
threads::{c_specpdl_index, ThreadState},
util::clip_to_bounds,
vectors::LispVectorlikeRef,
};

Expand Down Expand Up @@ -1653,8 +1653,8 @@ pub fn window_hscroll(window: LispWindowLiveOrSelected) -> EmacsInt {
win.hscroll as EmacsInt
}

// Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
// range, returning the new amount as a fixnum.
/// Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
/// range, returning the new amount as a fixnum.
#[no_mangle]
pub extern "C" fn set_window_hscroll(mut w: LispWindowRef, hscroll: EmacsInt) -> EmacsInt {
// Horizontal scrolling has problems with large scroll amounts.
Expand All @@ -1667,7 +1667,7 @@ pub extern "C" fn set_window_hscroll(mut w: LispWindowRef, hscroll: EmacsInt) ->
Ok(mpf) => cmp::max(mpf, isize::max_value()),
Err(_) => isize::max_value(),
};
let new_hscroll = clip_to_bounds(0, hscroll, hscroll_max);
let new_hscroll = num::clamp(hscroll as isize, 0, hscroll_max);

// Prevent redisplay shortcuts when changing the hscroll.
if w.hscroll != new_hscroll {
Expand Down