digitalmars.D.learn - Why I'm getting this "Range Violation" error?
- rempas (19/19) Jul 09 2021 I have the following code:
- Mike Parker (13/17) Jul 09 2021 Because you're indexing the prompt with i before you ensure that
- rempas (4/22) Jul 09 2021 OMG!!! Yes! I feel bad for not catching it now... Meanwhile I
- zjh (2/3) Jul 09 2021 `prompt[i]!='{'`,here,i=len.so `array overflow`
- rempas (2/6) Jul 09 2021 Thanks a lot. It seems I have a lot to learn. Have an amazing day!
- =?UTF-8?Q?Ali_=c3=87ehreli?= (32/33) Jul 09 2021 In addition to what others said, you can take advantage of ranges to
- rempas (3/7) Jul 09 2021 Of course another help from the legend himself ;) As always
I have the following code: ``` import core.stdc.stdio; void print(T)(string prompt, T args...) { size_t len = prompt.length; size_t i = 0; while (prompt[i] != '{' && i < len) { printf("%c", prompt[i]); i++; } } void main() { print("Hello, world!\n", 10); } ``` When I execute it, I'm getting a range violation error. If I try to set "len" to be the length of the "prompt" minus 1, then it will work and it will print the "prompt" until the questionmark. So I cannot find where the error is...
Jul 09 2021
On Friday, 9 July 2021 at 07:21:06 UTC, rempas wrote:When I execute it, I'm getting a range violation error. If I try to set "len" to be the length of the "prompt" minus 1, then it will work and it will print the "prompt" until the questionmark. So I cannot find where the error is...Because you're indexing the prompt with i before you ensure that i is valid. Swap the operands in your if condition: ``` while (i < len && prompt[i] != '{') ``` Or better yet, use foreach, which exists to avoid this sort of mistake: ``` foreach(c; prompt) { if(c != '{') printf("%c", c); } ```
Jul 09 2021
On Friday, 9 July 2021 at 07:38:50 UTC, Mike Parker wrote:On Friday, 9 July 2021 at 07:21:06 UTC, rempas wrote:OMG!!! Yes! I feel bad for not catching it now... Meanwhile I realized "while" will not work in my example anyway. Thanks for the man! Have a nice day!When I execute it, I'm getting a range violation error. If I try to set "len" to be the length of the "prompt" minus 1, then it will work and it will print the "prompt" until the questionmark. So I cannot find where the error is...Because you're indexing the prompt with i before you ensure that i is valid. Swap the operands in your if condition: ``` while (i < len && prompt[i] != '{') ``` Or better yet, use foreach, which exists to avoid this sort of mistake: ``` foreach(c; prompt) { if(c != '{') printf("%c", c); } ```
Jul 09 2021
On Friday, 9 July 2021 at 07:21:06 UTC, rempas wrote:I have the following code:`prompt[i]!='{'`,here,i=len.so `array overflow`
Jul 09 2021
On Friday, 9 July 2021 at 07:54:44 UTC, zjh wrote:On Friday, 9 July 2021 at 07:21:06 UTC, rempas wrote:Thanks a lot. It seems I have a lot to learn. Have an amazing day!I have the following code:`prompt[i]!='{'`,here,i=len.so `array overflow`
Jul 09 2021
On 7/9/21 12:21 AM, rempas wrote:while (prompt[i] != '{' && i < len) {In addition to what others said, you can take advantage of ranges to separate concerns of filtering and iteration. Here are two ways: import core.stdc.stdio; import std.algorithm; // Same as your original void print(T)(string prompt, T args...) { prompt .filter!(c => c != '{') .each!(c => printf("%c", c)); // If you are not familiar with the shorthand lambda syntax, // I read c => foo(c) as "given c, produce foo(c)." // // Well... 'each' is different because it does not // produce anything. It its case: "given c, do this." } // This one dispatches filtering to another function but // still uses a 'foreach' loop void print2(T)(string prompt, T args...) { foreach (c; prompt.sansCurly) { printf("%c", c); } } // I used R instead of 'string' in case it will be more useful auto sansCurly(R)(R range) { return range.filter!(c => c != '{'); } void main() { print("Hello, {world!\n", 10); print2("Hello, {world!\n", 10); } Ali
Jul 09 2021
On Friday, 9 July 2021 at 11:56:40 UTC, Ali Çehreli wrote:In addition to what others said, you can take advantage of ranges to separate concerns of filtering and iteration. Here are two ways: [...]Of course another help from the legend himself ;) As always thanks a lot and have an amazing day!
Jul 09 2021