From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id EAAD2BC6B for ; Tue, 22 Jan 2008 09:12:38 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAACQ0lUfC2fJVlmdsb2JhbACQGgEBAQEHBAYHChEHmmk X-IronPort-AV: E=Sophos;i="4.25,232,1199660400"; d="scan'208";a="8145068" Received: from anchor-post-35.mail.demon.net ([194.217.242.85]) by mail3-smtp-sop.national.inria.fr with ESMTP; 22 Jan 2008 09:12:38 +0100 Received: from orion.metastack.com ([80.177.38.218]) by anchor-post-35.mail.demon.net with esmtp (Exim 4.67) id 1JHEFJ-0008Ps-Iw for caml-list@yquem.inria.fr; Tue, 22 Jan 2008 08:12:37 +0000 Received: from countertenor ([81.253.92.96]) (authenticated bits=0) by orion.metastack.com (8.13.4/8.13.3) with ESMTP id m0M7wHsr014004 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO) for ; Tue, 22 Jan 2008 07:58:19 GMT From: "David Allsopp" To: "OCaml List" Subject: "Safe" unsafe string access Date: Tue, 22 Jan 2008 09:12:20 +0100 Organization: MetaStack Solutions Ltd. Message-ID: <000f01c85cce$8a9a1030$989017ac@countertenor> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 Thread-Index: AchajnIVczhTz3cIT3KnNeipx21a7w== X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 X-Scanned-By: MIMEDefang 2.63 on 172.16.28.218 X-Spam: no; 0.00; ocaml:01 parsing:01 functions:01 unsafe:01 unsafe:01 motivation:02 string:02 string:02 library:03 character:04 null:05 i'd:06 long:06 000:92 000:92 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)