Overview  Index  Help 
SMLDoc

Utility


structure Utility =
struct
  exception MissingEscapedChar
  val interleave
  val interleaveString
  val isPrefixOf
  val splitLast
  val sort
  val uniq
  val satisfyAny
  val satisfyAll
  val compareStringNoCase
  val tokenizeString
  val replaceString
  val replaceStringByTable
  val replaceFile
end

This module provides functions independent of the specific applications.

Author:
YAMATODANI Kiyoshi
Version:
$Id: StrUtility.html,v 1.9 2007/02/17 07:01:58 kiyoshiy Exp $

       
Value detail

interleave

fun interleave separator list

interleave elements of a list with separators.

Parameters:
separator
the separator
list
the list
Returns:
the list interleaved with separators

interleaveString

fun interleaveString separator strings

Concat strings with separator.

String.concatWith in the SMLBasis library provides the same functionality, but this function is not implemented by some version of SML/NJ.

Parameters:
separator
the separator
string
the list of string
Returns:
the concatenation of the strings using the separator

isPrefixOf

fun isPrefixOf (left, right)

indicates whether a list is a prefix of another list.

Parameters:
left
left list
right
right list
Returns:
true if the left is a prefix of the right.

splitLast

fun splitLast list

split a list into a pair of the elements but the last element and the last element.

Example:

splitLast [1, 2, 3, 4]
returns
([1, 2, 3], 4)

Parameters:
list
a list
Returns:
a pair of first n-1 elements and the last element if the list has n elements.

sort

fun sort comparator list

sorts a list.

ToDo : use efficient algorithm.

Parameters:
comparator
a function which compares two elements in the list. When applied to (left, right), it must return true if left < right.
list
the list to be sorted.
Returns:
the sorted list

uniq

fun uniq list

filter out repeated elements in a list.

Parameters:
list
sorted list
Returns:
a list in which adjacent equal elements are merged

satisfyAny

fun satisfyAny conditions value

build a predicator which is a disjunction of predicators.

Parameters:
conditions
a list of predicator whose type is 'a -> bool
value
the value to be examined.
Returns:
true if any of conditions returns true when applied to the value.

satisfyAll

fun satisfyAll conditions value

build a predicator which is a conjunction of predicators.

Parameters:
conditions
a list of predicator whose type is 'a -> bool
value
the value to be examined.
Returns:
true if all of conditions return true when applied to the value.

compareStringNoCase

fun compareStringNoCase (left, right)

case-insensitive version of String.collate

Parameters:
left
left string
right
right string
Returns:
order

tokenizeString

fun tokenizeString isDelimiter string

break a string into tokens (with escape interpretation facility).

Example:

 tokenizeString Char.isSpace " --header=Generated\\ by\\ SMLDoc\\ 1.0 foo.sml "
 
returns
["--header=Generated by SMLDoc 1.0","foo.sml"]
 
A charcter following a backslash is treated as an ordinary char.

Parameters:
isDelimiter
a function which receive a character and returns true if it is a delimiter char.
string
a string
Exception:
MissingEscapedChar
if the string ends a backslash which is not escaped by preceding another backslash.

replaceString

fun replaceString oldString newString string

replaces string.

example:

 - Utility.replaceString "foo" "bar" "fooboofooboofoo";
 val it = (3,"barboobarboobar") : int * string
 

Parameters:
oldString
the string to be replaced
newString
the string to be inserted
Returns:
a pair of
  • the number of replace performed
  • the string in which occurrences of oldString are replaced with newString

replaceStringByTable

val replaceStringByTable


replaceFile

fun replaceFile keyValuePairs (srcFileName, destFileName)

replaces strings in the contents of a file. This function reads the contents of the source file and replaces the occurrences of the first element of a pair in the keyValuePairs with the second element of that pair. The result of replace is output to the destination file.

Parameters:
keyValuePairs
a list of pairs of oldString and newString
srcFileName
the name of source file
destFileName
the name of destination file
Returns:
unit

 
Exception detail

MissingEscapedChar

exception MissingEscapedChar

raised by the tokenizeString if the string ends with a unescaped backslash.


Overview  Index  Help 
SMLDoc: Documentation generator for SML