flat assembler
Message board for the users of flat assembler.

Index > Main > design of substring routine [FASMLIB]


Return what?
Empty string and ZF=1
63%
 63%  [ 7 ]
Error
36%
 36%  [ 4 ]
Total Votes : 11

Author
Thread Post new topic Reply to topic
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 14 Aug 2006, 16:42
hi, i am designing substring routine (actually called "str.sub") for FASMLIB, and i have encountered one problem.

First i explain what str.sub does. It takes string as argument, and two numbers: index and len, and then extracts substring from string, starting and position index, taking len chars.

If index+len overlaps length of string, then functions returns with OF set, and returned string contains less than len characters.

But i ran into problem, when index is higher than length of string (or equal...). Then returned string is empty, eg. contains only ending null.

For now, i DO return empty string, and also return with ZF=1 so caller can easily distinguish this case. This solution is more general, and sometimes returning empty string can be awaited behavior.

Another posibility is to return with error in this case, probably more suitable in most cases, but when the other behavior is wanted, caller has to check index himself and create empty string himself, opposed to checking ZF for every str.sub

which one do you find better?
Post 14 Aug 2006, 16:42
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM 14 Aug 2006, 17:07
I think that ZF should be set and an empty string should be returned, since it is not hard to do a second 'jz error' in case the application wants to consider it an error. Also you could set a compile-time flag to switch between behaviours.
Post 14 Aug 2006, 17:07
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 14 Aug 2006, 17:14
surely no compile-time flag Wink
too much generality causes confusion.

thanks for opinion
Post 14 Aug 2006, 17:14
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
MazeGen



Joined: 06 Oct 2003
Posts: 977
Location: Czechoslovakia
MazeGen 14 Aug 2006, 17:25
It should return an error because:

  1. index is completely out of reasonable bound
  2. the caller can always check them itself before calling - don't put too much responsibility over the function
  3. don't make the function too smart Wink


UCM, you forgot to vote.
Post 14 Aug 2006, 17:25
View user's profile Send private message Visit poster's website Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22 14 Aug 2006, 21:56
I think you should consider returning an empty string as well as set the zf, so that your libraries functions work fluently with eachother.

While error checking is important and part of good coding practice it should not be mandatory.

For example, what if someone wishes to append the returned string from your substring function to a pre existing string and the person doesn't mind of the returned substring has no characters in it? You are forcing them to use unneeded error checking logic. This error checking logic becomes even more exagerated if the returned strings of multiple calls to sub are appended together into one string.

If the purpose of this lib is to emulate high level string functionality using assembly then you should allow such high level approaches as
IE
strNew = strT1.substr(4, 3) + strT2.substr(3, 1) + strT3.substr(7, 10);
If strT2's sub string is a null the appending still works because a null string is appended to strNew.
Post 14 Aug 2006, 21:56
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
ChrisLeslie



Joined: 04 Jun 2006
Posts: 50
Location: Australia
ChrisLeslie 18 Aug 2006, 01:33
vid

My preference would be to make you routine as robust as possible by detecting any breach of the string's bounds at the start of the routine. Therefore, at least, if both index OR index+len are either before OR after the string's bounds then return a null string and particular flags set. I don't like the idea of returning an incomplete (less than len) string. I would then leave it up to the main application to optionaly examine the flags and act as appropriate to the circumstances.
BTW, will you call the first position in the string as index=0, or index=1? Just curious.

Chris
Post 18 Aug 2006, 01:33
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 18 Aug 2006, 08:04
Quote:
BTW, will you call the first position in the string as index=0, or index=1?

of course index 0 - it's "behind 0th char", like 1 is "behind 1st char" etc. making it otherwise will surely result in some incrementing and decrementing. btw, this number is same as index into array, when accessing memory. But thanks for question, i have to add this to docs, i thought it is obvious enough.

about the non-flags - maybe i could design another routine that will surely return entire substring, and another that will return partial/empty string also. but i don't have any idea abou how to name them. also, checking one flag per call isn't that much, but you are right most of time it's annoying.
Post 18 Aug 2006, 08:04
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.