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
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLexport mlsize_t caml_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);
|
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_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));
|
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_create_string(value len)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
mlsize_t size = Long_val(len);
|
2004-01-01 08:42:43 -08:00
|
|
|
if (size > Bsize_wsize (Max_wosize) - 1){
|
|
|
|
caml_invalid_argument("String.create");
|
|
|
|
}
|
2003-12-29 14:15:02 -08:00
|
|
|
return caml_alloc_string(size);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_string_get(value str, value index)
|
1995-06-15 01:09:30 -07:00
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat idx = Long_val(index);
|
2004-01-02 11:23:29 -08:00
|
|
|
if (idx < 0 || idx >= caml_string_length(str)) caml_array_bound_error();
|
1995-06-15 01:09:30 -07:00
|
|
|
return Val_int(Byte_u(str, idx));
|
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_string_set(value str, value index, value newval)
|
1995-06-15 01:09:30 -07:00
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat idx = Long_val(index);
|
2004-01-02 11:23:29 -08:00
|
|
|
if (idx < 0 || idx >= caml_string_length(str)) caml_array_bound_error();
|
1995-06-15 01:09:30 -07:00
|
|
|
Byte_u(str, idx) = Int_val(newval);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_string_equal(value s1, value s2)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2007-01-30 01:52:08 -08:00
|
|
|
mlsize_t sz1, sz2;
|
1995-07-10 02:48:27 -07:00
|
|
|
value * p1, * p2;
|
2007-01-30 01:52:08 -08:00
|
|
|
|
|
|
|
if (s1 == s2) return Val_true;
|
|
|
|
sz1 = Wosize_val(s1);
|
|
|
|
sz2 = Wosize_val(s2);
|
1995-07-10 02:48:27 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_string_notequal(value s1, value s2)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2003-12-16 10:09:44 -08:00
|
|
|
return Val_not(caml_string_equal(s1, s2));
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
2003-04-01 00:46:39 -08:00
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_string_compare(value s1, value s2)
|
2003-04-01 00:46:39 -08:00
|
|
|
{
|
2004-05-17 10:10:00 -07:00
|
|
|
mlsize_t len1, len2;
|
2003-04-01 00:46:39 -08:00
|
|
|
int res;
|
|
|
|
|
2007-01-30 01:52:08 -08:00
|
|
|
if (s1 == s2) return Val_int(0);
|
2003-12-16 10:09:44 -08:00
|
|
|
len1 = caml_string_length(s1);
|
|
|
|
len2 = caml_string_length(s2);
|
2003-04-01 00:46:39 -08:00
|
|
|
res = memcmp(String_val(s1), String_val(s2), len1 <= len2 ? len1 : len2);
|
|
|
|
if (res < 0) return Val_int(-1);
|
|
|
|
if (res > 0) return Val_int(1);
|
|
|
|
if (len1 < len2) return Val_int(-1);
|
|
|
|
if (len1 > len2) return Val_int(1);
|
|
|
|
return Val_int(0);
|
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_string_lessthan(value s1, value s2)
|
2003-04-01 00:46:39 -08:00
|
|
|
{
|
2003-12-16 10:09:44 -08:00
|
|
|
return caml_string_compare(s1, s2) < Val_int(0) ? Val_true : Val_false;
|
2003-04-01 00:46:39 -08:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_string_lessequal(value s1, value s2)
|
2003-04-01 00:46:39 -08:00
|
|
|
{
|
2003-12-16 10:09:44 -08:00
|
|
|
return caml_string_compare(s1, s2) <= Val_int(0) ? Val_true : Val_false;
|
2003-04-01 00:46:39 -08:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_string_greaterthan(value s1, value s2)
|
2003-04-01 00:46:39 -08:00
|
|
|
{
|
2003-12-16 10:09:44 -08:00
|
|
|
return caml_string_compare(s1, s2) > Val_int(0) ? Val_true : Val_false;
|
2003-04-01 00:46:39 -08:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_string_greaterequal(value s1, value s2)
|
2003-04-01 00:46:39 -08:00
|
|
|
{
|
2003-12-16 10:09:44 -08:00
|
|
|
return caml_string_compare(s1, s2) >= Val_int(0) ? Val_true : Val_false;
|
2003-04-01 00:46:39 -08:00
|
|
|
}
|
1995-07-10 02:48:27 -07:00
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_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
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_fill_string(value s, value offset, value len, value init)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2003-04-01 00:46:39 -08:00
|
|
|
memset(&Byte(s, Long_val(offset)), Int_val(init), Long_val(len));
|
1995-06-18 07:44:56 -07:00
|
|
|
return Val_unit;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_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
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_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)));
|
|
|
|
}
|
|
|
|
|