Improving my spin?
jmspaggi
Posts: 629
Hi dear spin experts folks.
I did a spin methode, and I think it can be improved. I'm "spinning" since only few months, so I'm not very good with that.
Just to learn, is there a way to improve this method?
The goal of this method is to call changeDirectory for each token is the received path.
Like if we receive /pathA/pathB, we need to call changeDirectory (pathA) then changeDirectory (pathB).
The received parameter can start or and with a / or a \
So we can receive pathA\pathB\ as well as \pathA\pathB
So far, it alway go from the root. But maybe the 1st one should go from the currend directory only.
So is there anywhere we can improve the Spin code below?
Thanks,
JM
I did a spin methode, and I think it can be improved. I'm "spinning" since only few months, so I'm not very good with that.
Just to learn, is there a way to improve this method?
The goal of this method is to call changeDirectory for each token is the received path.
Like if we receive /pathA/pathB, we need to call changeDirectory (pathA) then changeDirectory (pathB).
The received parameter can start or and with a / or a \
So we can receive pathA\pathB\ as well as \pathA\pathB
So far, it alway go from the root. But maybe the 1st one should go from the currend directory only.
So is there anywhere we can improve the Spin code below?
Thanks,
JM
PUB changePath(path) | i result := 0 if byte[path] == "/" or byte[path] == "\" result := 1 i := result changeDirectory(string("/")) repeat until result == strsize(path) if ((byte[path+result] == "/") or (byte[path+result] == "\")) bytemove (@changePathNameBuffer, path+i, result-i) byte[@changePathNameBuffer+result-i] := 0 changeDirectory(@changePathNameBuffer) i := result+1 result++ return listWorkingDirectory
Comments
So in fact your path has to end with a "/" or a "\". Or you have to change the code to also call changeDirectory in case the loop is exited and i is not equal to result+1.
Another improvement would be to use the passed path instead of doing a bytemove. You can simply replace the found "\" or "/" with a $00. Then you have a valid string that you can use directly. Youd then call changeDirectory with path+i as you do it in the bytemove. If the path string should not be destroyed you can later on replace the $00 with "\" or "/".
Also, you're right. Look like it's not working if the path is not ending by "/" and "\". I will have to fix that too!
Thanks,
JM