/* Random utility Lisp functions.
Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997,
1998, 1999, 2000, 2001, 2002, 2003, 2004,
- 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+ 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
This file is part of GNU Emacs.
doc: /* Return a pseudo-random number.
All integers representable in Lisp are equally likely.
On most systems, this is 29 bits' worth.
-With positive integer argument N, return random number in interval [0,N).
-With argument t, set the random number seed from the current time and pid. */)
- (n)
- Lisp_Object n;
+With positive integer LIMIT, return random number in interval [0,LIMIT).
+With argument t, set the random number seed from the current time and pid.
+Other values of LIMIT are ignored. */)
+ (limit)
+ Lisp_Object limit;
{
EMACS_INT val;
Lisp_Object lispy_val;
unsigned long denominator;
- if (EQ (n, Qt))
+ if (EQ (limit, Qt))
seed_random (getpid () + time (NULL));
- if (NATNUMP (n) && XFASTINT (n) != 0)
+ if (NATNUMP (limit) && XFASTINT (limit) != 0)
{
/* Try to take our random number from the higher bits of VAL,
not the lower, since (says Gentzel) the low bits of `random'
it's possible to get a quotient larger than n; discarding
these values eliminates the bias that would otherwise appear
when using a large n. */
- denominator = ((unsigned long)1 << VALBITS) / XFASTINT (n);
+ denominator = ((unsigned long)1 << VALBITS) / XFASTINT (limit);
do
val = get_random () / denominator;
- while (val >= XFASTINT (n));
+ while (val >= XFASTINT (limit));
}
else
val = get_random ();
}
result_len += len;
+ if (result_len < 0)
+ error ("String overflow");
}
if (! some_multibyte)
}
DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0,
- doc: /* Return a substring of STRING, starting at index FROM and ending before TO.
-TO may be nil or omitted; then the substring runs to the end of STRING.
-FROM and TO start at 0. If either is negative, it counts from the end.
-
-This function allows vectors as well as strings. */)
+ doc: /* Return a new string whose contents are a substring of STRING.
+The returned string consists of the characters between index FROM
+\(inclusive) and index TO (exclusive) of STRING. FROM and TO are
+zero-indexed: 0 means the first character of STRING. Negative values
+are counted from the end of STRING. If TO is nil, the substring runs
+to the end of STRING.
+
+The STRING argument may also be a vector. In that case, the return
+value is a new vector that contains the elements between index FROM
+\(inclusive) and index TO (exclusive) of that vector argument. */)
(string, from, to)
Lisp_Object string;
register Lisp_Object from, to;
else if (EQ (item, Qdays)) /* e.g. for calendar-day-name-array */
{
Lisp_Object v = Fmake_vector (make_number (7), Qnil);
- int days[7] = {DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7};
+ const int days[7] = {DAY_1, DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7};
int i;
+ struct gcpro gcpro1;
+ GCPRO1 (v);
synchronize_system_time_locale ();
for (i = 0; i < 7; i++)
{
code_convert_string_norecord (val, Vlocale_coding_system,
0));
}
+ UNGCPRO;
return v;
}
#endif /* DAY_1 */
#ifdef MON_1
else if (EQ (item, Qmonths)) /* e.g. for calendar-month-name-array */
{
- struct Lisp_Vector *p = allocate_vector (12);
- int months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7,
- MON_8, MON_9, MON_10, MON_11, MON_12};
+ Lisp_Object v = Fmake_vector (make_number (12), Qnil);
+ const int months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7,
+ MON_8, MON_9, MON_10, MON_11, MON_12};
int i;
+ struct gcpro gcpro1;
+ GCPRO1 (v);
synchronize_system_time_locale ();
for (i = 0; i < 12; i++)
{
str = nl_langinfo (months[i]);
val = make_unibyte_string (str, strlen (str));
- p->contents[i] =
- code_convert_string_norecord (val, Vlocale_coding_system, 0);
+ Faset (v, make_number (i),
+ code_convert_string_norecord (val, Vlocale_coding_system, 0));
}
- XSETVECTOR (val, p);
- return val;
+ UNGCPRO;
+ return v;
}
#endif /* MON_1 */
/* LC_PAPER stuff isn't defined as accessible in glibc as of 2.3.1,