caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: yjc01@doc.ic.ac.uk
To: caml-list@inria.fr
Subject: index of substring
Date: Thu, 27 Jan 2005 00:41:18 +0000	[thread overview]
Message-ID: <1106786478.41f838aecb7dd@www.doc.ic.ac.uk> (raw)

I tried to write a function to return the index of a substring in a string from
an index. So I can use it to extract some comments (text between (* and *) in a
file. But came across some problem when returning the value.

The code I came up with is: 
(* Use Unix Module *)
open Unix;;


(* get the file size *)
let fileReader = openfile "student.cd" [O_RDONLY] 0o640;;
let fileSize = (fstat fileReader).st_size;;
print_int fileSize;;

(* create variable to store file *)
let comments = String.make fileSize 'c';;

 
let read_char () = 
	read fileReader comments 0 fileSize;;


(* return index of a substring in a string from an index, if substring not
matched retun -1 *)
let index_left fromIndex string findString = 
	(* Step 1. Find first character *)
	let firstIndex = String.index_from string fromIndex (String.get findString 0) in
	
	   if (firstIndex >= 0)	then
		let isMatched = ref true in
		for i = 1 to (String.length findString) do
			if (String.get string firstIndex) != (String.get findString 0) 
			   then !isMatched = false;
			if (!isMatched)
			   then firstIndex 
			   else -1;
			
		done;;
				
(* test if index_left works *)
let check index = 
	 index_left index comments "(*" 
		

print_int check 1;;


Or is there an easier way to do this?

Daisy


             reply	other threads:[~2005-01-27  0:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-27  0:41 yjc01 [this message]
2005-01-27  4:21 ` [Caml-list] " Matt Gushee
2005-01-27  6:44 ` Radu Grigore
2005-01-27  9:36   ` skaller
2005-01-27 10:20     ` Jean-Christophe Filliatre
2005-01-27 16:55       ` skaller
2005-01-27 17:20         ` Radu Grigore
2005-01-28  8:51         ` Jean-Christophe Filliatre
2005-01-28 13:31           ` skaller
2005-01-27  8:37 ` Oliver Bandel
2005-01-27  9:17 ` Xavier Leroy

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=1106786478.41f838aecb7dd@www.doc.ic.ac.uk \
    --to=yjc01@doc.ic.ac.uk \
    --cc=caml-list@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;
as well as URLs for NNTP newsgroup(s).