6 ms·
For anyone wondering, '1' * 4301 creates a string of '11111....' 4301 characters long. It doesn't result in an integer value of 4301 like in some other language
by googlryas 4y ago
For anyone wondering, '1' * 4301 creates a string of '11111....' 4301 characters long. It doesn't result in an integer value of 4301 like in some other languages.
I find this a strange modification to the language, though probably not a particularly painful one. Has python saved you from yourself when dealing with non-linear built-in algorithms before? IIRC it is also possible to have the regex engine take an inordinate amount of time for certain matching concepts(I think stackoverflow was affected by this?), but the engine wasn't hobbled to throw in those cases, it is merely up to the user to write efficient regex that aren't subject to those problems.
- hyperpape 4y agoBacktracking regular expressions as an intentional or accidental DOS vector are a moderately well-known issue, and while I prefer that a standard library implementation be robust against them, I can see the POV that it's buyer beware. Converting a string to an integer is somewhat less well known as a DOS vector, more painful to avoid as an application creator, and easier to fix in code. So there's a cost-benefit argument that you should just do this before you rewrite your regex engine.
- masklinn 4y ago> I can see the POV that it's buyer beware. On the other hands, lots of buyers are not aware that it's an issue, and more frustratingly there are regex engines which are very resilient to it... but are not widely used. Python's stdlib will fall over on any exponential backtracking pattern, but last time I tried to make postgres fall over I didn't succeed. Even though it does have lookahead, lookbehind, and backrefs, so should be sensible to the issue (aka it's not a pure DFA).
- bo1024 4y agoThis does seem like a strange level of handholding, even if the motivation makes lots of sense. If you start going down the road of protecting people who don't sanitize user input, you may have quite a long journey ahead...
- mjevans 4y agoOperator overloading sure seems to increase the prevalence of foot-guns, security issues, and other gotchas. str.ccClone(4301) # ConCatenate Clones of the source string N times. Would even an abbreviated, named, function not be more self documenting and better for human and machine reviews?
- slaymaker1907 4y agoI think how Rust does it is fine, but I agree operators are often a mess. Yesterday I was looking at a memory dump where there was a problem in a destructor (a double free was detected) and it was an absolute mess trying to figure out the exact execution location in source code since it was setting the value of a smart pointer which triggered a decrement of a reference counted value in turn triggering a free. It's junk like that which starts to convince me that Linus was right to avoid C++. Rust obviously also has destructors, but it doesn't have the nightmare that is inheritance+function overloading+implicit casting.
- cma 4y ago> and it was an absolute mess trying to figure out the exact execution location in source code since it was setting the value of a smart pointer which triggered a decrement of a reference counted value in turn triggering a free. Isn't all that context there in the stack trace?
- jlarocco 4y agoYes, probably. Depends on the compiler settings. Stuff can get optimized out and stripped. When writing the code in the first place, though, it's difficult to see problems like that because it's all hidden behind magic calls to copy constructors, move semantics, and destructor calls. Out of sight, out of mind.
- DSMan195276 4y agoI think it's separate from his point but some of those things could potentially be tail calls, meaning the functions actually leading to the free/delete might not be in the stacktrace even if they were called.
- gsliepen 4y agoWell, in C++ int('1' * 4301) is a perfectly valid expression, but it evaluates to 210749, not 4301.
- oldgradstudent 4y agoOr some other value. If sizeof(int)=2, the result is undefined.
- gsliepen 4y agoNot if CHAR_BIT is 10 or more!
- oldgradstudent 4y agoI wonder how much software will fail on platforms where CHAR_BIT is not 8.
- Kamq 4y agoI've seen 2 programs in my life that checked for CHAR_BIT (and one was a toy decimal -> binary converter). My guess is basically all of it.
- AlotOfReading 4y agoI worked with some MCUs whose only redeeming quality was their cost that had CHAR_BIT=16. The answer is basically everything. It made string processing was profoundly annoying, even for C.
- colejohnson66 4y agoIs it one of TI’s custom ISAs for their DSP line? The TMS430, for example, has a 16 bit char. That causes some unintuitive looking code to fight that.
- 4y ago
- ffhhj 4y agoThey should have made the analogous inverse operation: '1234' / 2 = ['12', '34']
- tremon 4y agoThat's not the inverse of the multiplication though. The inverse would be '33' / 2 = '3', and '1234'/2 should then probably raise a ValueError.
- bsdz 4y agoI was more expecting '1111' / 4 = '1'. This would be the inverse operation. However, it opens up even more questions like what to do if your string has mixed values etc
- ffhhj 4y agoThe string multiplication is about _joining_ strings, the inverse is about _splitting_ them in several parts. It's only confusing because the * appends the string to itself, the / is actually very clear.
- dekhn 4y agoDisagree. The inverse "string" * value is logically splitting, and then collapsing the repeated values. The logical split can be omitted, but the collapsing cannot.
- jbverschoor 4y agoNaming is hard they say
- deleted 4y ago[deleted]
- jejones3141 4y agoIn Algol 68, you can do that; it's part of the standard prelude. I think that some people who'd worked on Algol 68 in the Netherlands also worked on the ABC language, where it's "1" ^^ 4301, and Guido worked on ABC before Python.