caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* "Safe" unsafe string access
@ 2008-01-22  8:12 David Allsopp
  0 siblings, 0 replies; only message in thread
From: David Allsopp @ 2008-01-22  8:12 UTC (permalink / raw)
  To: OCaml List

Without inviting any opinions on the *sensibility* of doing this, given the
internal representation of the empty string in OCaml, could I take

String.unsafe_get str 0

to be safe for any string? In the empty-string case, I'm thinking it will
always return '\000' accessing the null terminator of the empty string which
is fine as long as I'm not testing for a real '\000' in a string!

My motivation comes from parsing code that test the first character of a
string. Normally, for example, I'd write:

if s <> "" && s.[0] = '$'
then ...

But I'm wondering whether I can safely change this to:

If String.unsafe_get s 0 = '$'
then ...

Correct?


David

(Normally I would never use unsafe_ functions unless writing a library
function where I have demonstrably already performed the necessary check
that the safe version of the function would perform)


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2008-01-22  8:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-22  8:12 "Safe" unsafe string access David Allsopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).