-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Sharedp and negative indices #58
Sharedp and negative indices #58
Conversation
515d93e
to
7b79c3a
Compare
@@ -9,6 +9,16 @@ | |||
(setf prove:*enable-colors* t) | |||
(plan nil) | |||
|
|||
(subtest "Index" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad commit
'(0 0 0 1 2 3 4 0 1 2 3 4 5 5 5) | ||
"index wraps and saturates")) | ||
|
||
(subtest "Slice" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs more tests
str.lisp
Outdated
(with-indices (index) (1+ (length s)) | ||
(concat (slice 0 index s t) | ||
(string string/char) | ||
(slice index (length s) s t)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use nil/t instead of (length s)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we need a warning!
btw, we could enrich the warning's message, to explain where it comes from, and what is this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks doable. With a warning first, and maybe with a key argument to control the behavior? If someone speaks up we can be more conservative.
I would appreciate a keyword to control the behavior. And, I am afraid code of mine would break so I am tempted to say, let the new behavior be optional and activated with the keyword argument.
Could we speak a bit more in the issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I am thinking about adding some if-out-of-bounds
keyword argument, we can talk more about it in the issue, if you have questions or preferences.
@@ -271,11 +271,22 @@ Examples: | |||
(is "" (substring 2 1 "abcd") "start is bigger than end") | |||
``` | |||
|
|||
#### slice `(start end s &optional sharedp)` | |||
|
|||
Same as substring, except the result might share storage with s. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and more importantly, what's a negative end
.
@@ -137,27 +151,126 @@ | |||
(defun version () | |||
(print +version+)) | |||
|
|||
;; as a dedicated condition to help detect it in tests | |||
;; (up to 0.18.1 a negative index would mean zero) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but not in all functions right? I have to check below.
str.lisp
Outdated
(t | ||
(locally (declare (type (and fixnum (not (eql 0))) length)) | ||
(when (< index 0) | ||
(warn 'negative-index)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary to warn for a new function? (it doesn't harm though)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a warnp keyword argument thay may eventually be removed later
str.lisp
Outdated
|
||
(defun index (string/length index) | ||
"" | ||
(declare (optimize (speed 3)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is necessary nor a good thing to hardcode this. Explain?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not necessary, might be better to remove declarations
str.lisp
Outdated
(if (< quotient 0) 0 length)))))))) | ||
|
||
(defmacro with-indices ((&rest indices) string &body body) | ||
"Ensure INDICES are computed according to INDEX w.r.t. STRING in BODY. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does INDEX come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function INDEX, updating comment
str.lisp
Outdated
(with-indices (index) (1+ (length s)) | ||
(concat (slice 0 index s t) | ||
(string string/char) | ||
(slice index (length s) s t)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks doable. With a warning first, and maybe with a key argument to control the behavior? If someone speaks up we can be more conservative.
I would appreciate a keyword to control the behavior. And, I am afraid code of mine would break so I am tempted to say, let the new behavior be optional and activated with the keyword argument.
Could we speak a bit more in the issue?
(if omit-nulls | ||
(remove-if (lambda (it) (empty? it)) res) | ||
(delete-if #'empty? res) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. delete-if is destructive, we want to create a new string.
str.lisp
Outdated
@@ -250,14 +356,14 @@ It uses `subseq' with differences: | |||
(let ((end (max (- len (length ellipsis)) | |||
0))) | |||
(setf s (concat | |||
(subseq s 0 end) | |||
(slice% 0 end s t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why, what does that solve, is it a breaking change?
ellipsis)))) | ||
s) | ||
|
||
(defun words (s &key (limit 0)) | ||
"Return list of words, which were delimited by white space. If the optional limit is 0 (the default), trailing empty strings are removed from the result list (see cl-ppcre)." | ||
(when s | ||
(cl-ppcre:split "\\s+" (trim-left s) :limit limit))) | ||
(cl-ppcre:split "\\s+" (trim-left s) :limit limit :sharedp *sharedp*))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add the keyword argument.
(defun prefix-1 (item1 item2) | ||
(subseq item1 0 (or (mismatch item1 item2) (length item1)))) | ||
(slice 0 (mismatch item1 item2) item1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
review to be continued.
There are too may changes. Please split the PR. Some little changes would be ok to merge. And we must discuss thanks! |
Allows functions to return displaced arrays when *SHAREDP* is T.
Maybe this should be turned into GitHub Issues instead
- Introduce INDEX to give a canonical index computation in strings - Introduce WITH-INDICES to help canonicalize input indices - Warns when indices are negative because the behaviour differs from previous versions. The warning is an explicit condition class named NEGATIVE-INDEX so that this case can be handled more easily by user code that has automatic tests in place. - Adjust test cases for INSERT
7b79c3a
to
1aad505
Compare
This pull request is related to the following issues:
There is probably still work to do, this is more a draft to talk about the changes