Commit c0145db9 authored by Bas Lijnse's avatar Bas Lijnse

Added the Text module which provides the basic operations on text

git-svn-id: https://svn.cs.ru.nl/repos/clean-platform/trunk@3 2afc29ad-3112-4e41-907a-9359c7e6e986
parent 701b4792
definition module Text
/**
* This module defines the basic operations on pieces of text
* It also gives an implementation for the String type which
* is assumed to be an ASCII string
*/
/**
* This class defines the basic operations for manipulating pieces of text.
*/
class Text s
where
/**
* Calculates the number of logical characters in a piece of text.
* When a multibyte encoding is used, this is less then the size in bytes.
*/
textSize :: !s -> Int
/**
* Splits a string into a list of strings using a separator string.
*
* @param The separator string.
* @param The string that is to be splitted.
*/
split :: !s !s -> [s]
/**
* Joins a list of strings using a separator string.
*
* @param The separator string.
* @param The string that is to be splitted.
*/
join :: !s ![s] -> s
/**
* Find the first occurence of a substring in another string.
*
* @param The search string.
* @param The string that is being searched.
*/
indexOf :: !s !s -> Int
/**
* Find the last occurence of a substring in another string.
*
* @param The search string.
* @param The string that is being searched.
*/
lastIndexOf :: !s !s -> Int
/**
* Predicate which tests if a string starts with another substring
*
* @param The substring.
* @param The string that is being searched.
*/
startsWith :: !s !s -> Bool
/**
* Predicate which tests if a string ends with another substring
*
* @param The substring.
* @param The string that is being searched.
*/
endsWith :: !s !s -> Bool
/**
* Take a substring from a string
*
* @param the logical start index.
* @param the logical length of the substring.
* @param the string from which the substring is taken.
*/
subString :: !Int !Int !s -> s
/**
* Replaces all occurences of a substring with another in a string
*
* @param The substring.
* @param The replacement.
* @param The string that is being searched.
*/
replaceSubString :: !s !s !s -> s
/**
* Removes whitespace from the beginning and end of a string.
*/
trim :: !s -> s
/**
* Removes whitespace from the beginning of a string.
*/
ltrim :: !s -> s
/**
* Removes whitespace from the end of a string.
*/
rtrim :: !s -> s
/**
* Converts all characters in a string to lower case.
*/
toLowerCase :: !s -> s
/**
* Converts all characters in a string to upper case.
*/
toUpperCase :: !s -> s
/**
* Instances of the text operations for plain ASCII strings.
*/
instance Text String
implementation module Text
import StdOverloaded, StdString, StdArray, StdChar, StdInt, StdBool, StdClass, StdList
instance Text String
where
textSize :: !String -> Int
textSize s = size s
split :: !String !String -> [String]
split sep s
# index = indexOf sep s
| index == -1 = [s]
= [s % (0, index - 1): split sep (s % (index + (size sep), size s))]
join :: !String ![String] -> String
join sep [] = ""
join sep [x:[]] = x
join sep [x:xs] = x +++ sep +++ (join sep xs)
indexOf :: !String !String -> Int
indexOf "" haystack = -1
indexOf needle haystack = `indexOf needle haystack 0
where
`indexOf needle haystack n
| (n + size needle) > (size haystack) = -1
| and [needle.[i] == haystack.[n + i] \\ i <- [0..((size needle) - 1)]] = n
= `indexOf needle haystack (n + 1)
lastIndexOf :: !String !String -> Int
lastIndexOf "" haystack = -1
lastIndexOf needle haystack = `lastIndexOf needle haystack (size haystack)
where
`lastIndexOf needle haystack n
| n < 0 = -1
| and [needle.[i] == haystack.[n + i] \\ i <- [0..((size needle) - 1)]] = n
= `lastIndexOf needle haystack (n - 1)
startsWith :: !String !String -> Bool
startsWith needle haystack = indexOf needle haystack == 0
endsWith :: !String !String -> Bool
endsWith needle haystack = lastIndexOf needle haystack == (size haystack) - (size needle)
subString :: !Int !Int !String -> String
subString start len haystack = haystack % (start, start + len - 1)
replaceSubString :: !String !String !String -> String
replaceSubString needle replacement haystack
| index == -1 = haystack
| otherwise = start +++ replacement +++ (replaceSubString needle replacement end)
where
index = indexOf needle haystack
start = subString 0 index haystack
end = subString (index + size needle) (size haystack) haystack
trim :: !String -> String
trim s = ltrim (rtrim s)
ltrim :: !String -> String
ltrim "" = ""
ltrim s
| isSpace s.[0] = if (size s == 1) "" (ltrim (s % (1, size s - 1)))
= s
rtrim :: !String -> String
rtrim "" = ""
rtrim s
| isSpace s.[size s - 1] = if (size s == 1) "" (rtrim (s % (0, size s - 2)))
= s
toLowerCase :: !String -> String
toLowerCase s = {toLower c \\ c <-: s}
toUpperCase :: !String -> String
toUpperCase s = {toUpper c \\ c <-: s}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment