Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "David Allsopp" <dra-news@metastack.com>
To: "OCaml List" <caml-list@yquem.inria.fr>
Subject: "Safe" unsafe string access
Date: Tue, 22 Jan 2008 09:12:20 +0100	[thread overview]
Message-ID: <000f01c85cce$8a9a1030$989017ac@countertenor> (raw)

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)


                 reply	other threads:[~2008-01-22  8:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='000f01c85cce$8a9a1030$989017ac@countertenor' \
    --to=dra-news@metastack.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox