1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* Operations on strings */
|
|
|
|
|
|
|
|
#include <string.h>
|
1999-05-15 08:08:11 -07:00
|
|
|
#include <ctype.h>
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "alloc.h"
|
|
|
|
#include "fail.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "misc.h"
|
1999-05-15 08:08:11 -07:00
|
|
|
#ifdef HAS_LOCALE
|
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport mlsize_t string_length(value s)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
mlsize_t temp;
|
|
|
|
temp = Bosize_val(s) - 1;
|
|
|
|
Assert (Byte (s, temp - Byte (s, temp)) == 0);
|
|
|
|
return temp - Byte (s, temp);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value ml_string_length(value s)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
mlsize_t temp;
|
|
|
|
temp = Bosize_val(s) - 1;
|
|
|
|
Assert (Byte (s, temp - Byte (s, temp)) == 0);
|
|
|
|
return Val_long(temp - Byte (s, temp));
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value create_string(value len)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
mlsize_t size = Long_val(len);
|
1997-06-16 10:07:11 -07:00
|
|
|
if (size > Bsize_wsize (Max_wosize) - 1) invalid_argument("String.create");
|
1995-05-04 03:15:53 -07:00
|
|
|
return alloc_string(size);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value string_get(value str, value index)
|
1995-06-15 01:09:30 -07:00
|
|
|
{
|
|
|
|
long idx = Long_val(index);
|
|
|
|
if (idx < 0 || idx >= string_length(str)) invalid_argument("String.get");
|
|
|
|
return Val_int(Byte_u(str, idx));
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value string_set(value str, value index, value newval)
|
1995-06-15 01:09:30 -07:00
|
|
|
{
|
|
|
|
long idx = Long_val(index);
|
|
|
|
if (idx < 0 || idx >= string_length(str)) invalid_argument("String.set");
|
|
|
|
Byte_u(str, idx) = Int_val(newval);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value string_equal(value s1, value s2)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
|
|
|
mlsize_t sz1 = Wosize_val(s1);
|
|
|
|
mlsize_t sz2 = Wosize_val(s2);
|
|
|
|
value * p1, * p2;
|
|
|
|
if (sz1 != sz2) return Val_false;
|
|
|
|
for(p1 = Op_val(s1), p2 = Op_val(s2); sz1 > 0; sz1--, p1++, p2++)
|
|
|
|
if (*p1 != *p2) return Val_false;
|
|
|
|
return Val_true;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value string_notequal(value s1, value s2)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
|
|
|
return Val_not(string_equal(s1, s2));
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value blit_string(value s1, value ofs1, value s2, value ofs2, value n)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(&Byte(s2, Long_val(ofs2)), &Byte(s1, Long_val(ofs1)), Int_val(n));
|
1995-06-18 07:44:56 -07:00
|
|
|
return Val_unit;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value fill_string(value s, value offset, value len, value init)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
register char * p;
|
|
|
|
register mlsize_t n;
|
|
|
|
register char c;
|
|
|
|
|
|
|
|
c = Long_val(init);
|
|
|
|
for(p = &Byte(s, Long_val(offset)), n = Long_val(len);
|
|
|
|
n > 0; n--, p++)
|
|
|
|
*p = c;
|
1995-06-18 07:44:56 -07:00
|
|
|
return Val_unit;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value is_printable(value chr)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
1999-05-15 08:08:11 -07:00
|
|
|
#ifdef HAS_LOCALE
|
|
|
|
static int locale_is_set = 0;
|
|
|
|
if (! locale_is_set) {
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
locale_is_set = 1;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1996-02-15 08:19:09 -08:00
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
c = Int_val(chr);
|
1999-05-15 08:08:11 -07:00
|
|
|
return Val_bool(isprint(c));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1996-04-04 07:56:26 -08:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value bitvect_test(value bv, value n)
|
1996-04-04 07:56:26 -08:00
|
|
|
{
|
|
|
|
int pos = Int_val(n);
|
|
|
|
return Val_int(Byte_u(bv, pos >> 3) & (1 << (pos & 7)));
|
|
|
|
}
|
|
|
|
|