View previous topic :: View next topic |
Author |
Message |
Arera
Joined: 23 Sep 2007 Posts: 386 Location: Wuppertal, Germany
|
Posted: Thu Jul 03, 2014 8:38 am Post subject: Strg_var = Left(Str_var , 0) Problem |
|
|
Hi,
I expect following to return an empty string:
Strg_var = "123"
Strg_var = Left(Str_var , 0)
But indeed it retutns Strg_var to be "1", just the same as
Strg_var = "123"
Strg_var = Left(Str_var , 1)
Is there a reason for that? The HELP says nothing about that...
Backround:
I shorten a string by receiving BACKSPACE characters, even down to empty the string.
Sure there are workarounds, but I wonder if LEFT works propper here.
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Jul 03, 2014 9:05 am Post subject: |
|
|
when you use a 0, it is in effect 255.
if you want a 0 check, you can change the code as below in mcs.lib:
[_COPYSTRING]
$EXTERNAL _ADD_NULL
; number of chars to copy in R16
; used by left and right.
; IMPORTANT DO NOT MOVE, should be under CopyMidString code
_CopyString:
TST r16 ; check for 0
BREQ _CopyStringExit ; when 0 bytes to copy exit
_CopyString1:
Ld _temp1,Z+ ; load byte
St X+,_temp1 ; store byte
Dec R16 ; decrease counter
Breq _CopyStringExit ;ready
Tst R24 ; is character zero
Brne _CopyString1 ; no,next byte
Ret ; was zero so null is added, could remove this line
_CopyStringExit:
Rjmp _Add_Null ; add null and exit
[END] _________________ Mark |
|
Back to top |
|
|
Deanus
Joined: 26 May 2006 Posts: 188 Location: Adelaide
|
Posted: Thu Jul 03, 2014 9:50 am Post subject: |
|
|
Hi Arera,
If you are removing characters with back space, wouldn't it be better to
varnum = Len(Strg_var0)
then when you have no characters in the string Varnum would equal zero.
Regards
Dean |
|
Back to top |
|
|
Arera
Joined: 23 Sep 2007 Posts: 386 Location: Wuppertal, Germany
|
Posted: Thu Jul 03, 2014 1:06 pm Post subject: |
|
|
Hi Mark,
thanks for your code, but I will never ever coax in the mcs.lib!
@Deanus: to dermin the lenght does not shorten the string!
The workaround is simple, but still the behavior of LEFT makes no sense to me...
That's how I do it now (watch the workaround-line):
If Asc(chr_read_conf) = &H08 Then 'Backspace received
If Len(config_in) > 0 Then 'only shorten if characters left
N = Len(config_in) - 1 'calculate new lenght
Config_in = Left(config_in , N) 'works, exept for N = 0
If N = 0 Then Config_in = "" 'WORKAROUND
Result = Tcpwrite(terminal_socket , "{008}") 'echo BS to terminal
End If
End If |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Jul 03, 2014 7:57 pm Post subject: |
|
|
if you do not want to change the lib you can do this too :
- create a new file with bascom or notepad , for example mylibcode.lib
- paste the code into this file
- save the file in the bascom LIB folder
- include the file to your code with $lib "mylibcode.lib"
now the code from your lib will be used. _________________ Mark |
|
Back to top |
|
|
Arera
Joined: 23 Sep 2007 Posts: 386 Location: Wuppertal, Germany
|
Posted: Thu Jul 03, 2014 8:10 pm Post subject: |
|
|
Thank you Mark for your effort,
But my one-line workround does the job.
And doesn't the msc.lib change on updates of bascom?
Marc |
|
Back to top |
|
|
|