Skip to content

Make charCodeAt safe, add unsafe versions of charAt, charCodeAt #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

charAt :: Number -> String -> Maybe Char

charCodeAt :: Number -> String -> Number
charCodeAt :: Number -> String -> Maybe Number

drop :: Number -> String -> String

Expand Down Expand Up @@ -104,4 +104,13 @@

split :: Regex -> String -> [String]

test :: Regex -> String -> Boolean
test :: Regex -> String -> Boolean


## Module Data.String.Unsafe

### Values

charAt :: Number -> String -> Char

charCodeAt :: Number -> String -> Number
16 changes: 9 additions & 7 deletions src/Data/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ module Data.String
fromChar :: Char -> String
fromChar = charString

foreign import charCodeAt
"function charCodeAt(i) {\
\ return function(s) {\
\ return s.charCodeAt(i); \
\ };\
\}" :: Number -> String -> Number
foreign import _charCodeAt
"function _charCodeAt(i, s, Just, Nothing) {\
\ if (i < 0 || i >= s.length) return Nothing;\
\ else return Just(s.charCodeAt(i));\
\}" :: forall a. Fn4 Number String (a -> Maybe a) (Maybe a) (Maybe Number)

charCodeAt :: Number -> String -> Maybe Number
charCodeAt n s = runFn4 _charCodeAt n s Just Nothing

foreign import fromCharArray
"function fromCharArray(a) {\
Expand Down Expand Up @@ -123,7 +125,7 @@ module Data.String
\ };\
\}" :: String -> String -> [String]

foreign import toCharArray
foreign import toCharArray
"function toCharArray(s) {\
\ return s.split('');\
\}" :: String -> [Char]
Expand Down
24 changes: 24 additions & 0 deletions src/Data/String/Unsafe.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Data.String.Unsafe
( charAt
, charCodeAt
) where

import Data.Char

foreign import charCodeAt
"""
function charCodeAt(i) {
return function(s) {
return s.charCodeAt(i);
};
}
""" :: Number -> String -> Number

foreign import charAt
"""
function charAt(i) {
return function(s) {
return s.charAt(i);
};
}
""" :: Number -> String -> Char