4 ms·
Don't manually parse those things. If your database provides a way to parse those (like PostgreSQL does), make use of it. Use what your programming language pro
by GlitchMr 8y ago
Don't manually parse those things. If your database provides a way to parse those (like PostgreSQL does), make use of it. Use what your programming language provides, and if it really doesn't provide the functionality to parse IPv6 addresses, use a library to do so.
For example, in Rust you can write this:
use std::error::Error;
use std::net::{AddrParseError, SocketAddr};
fn parse_address(addr: &str) -> Result<SocketAddr, AddrParseError> {
addr.parse()
.or_else(|_| Ok(SocketAddr::new(addr.parse()?, 10443)))
}
fn main() -> Result<(), Box<dyn Error>> {
let addresses = [
"[2001:0db8:f00f::0553:1211:0088]:10444",
"2001:0db8:f00f::0553:1211:0088",
];
for address in &addresses {
println!("{:?}", parse_address(address)?);
}
Ok(())
}
This isn't specific to IPv6 by the way. It also applies to other standards like CSV (although I suppose with CSV it does vary, I saw so many broken CSV files that sometimes a custom implementation is the best way to go to parse those).
- mavhc 8y agoThe most stupid thing about CSV is it's entirely pointless, ASCII has codepoints for file, group, record and unit separators.
- twic 8y agoWhich you can't type on a keyboard, sensibly print on a terminal, or express with a symbolic escape (like '\n') in any language i know. If history had gone differently, the separators would be useful for separating things in files, but it didn't.
- JdeBP 8y agoI am sure I am not alone at chuckling at the idea that one cannot type FS, GS, RS, and US on a keyboard, clearly borne of GUI conventions and thinking. Some of us have been typing control characters on a keyboard for quite a number of years. This is what the [Control] key is for. (-: FS enjoys quite significant use nowadays on terminal keyboards in the worlds of Unix and Linux, it being the usual special character mapped to SIGQUIT. GS and RS are similarly fairly mainstream commands in VIM/NeoVIM.
- adwn 8y ago> I am sure I am not alone at chuckling at the idea [...] You might want to hold back on your condescending laughter for a bit. Neither vim nor nano allow me to insert a control character by pressing Ctrl+key, so how am I supposed enter the FS character when editing a text file? Besides, there are other operating systems besides Unix and Linux (cough Windows cough).
- jodoherty 8y agoActually, VIM does support this. Type Ctrl+V in insert mode followed by whatever you want to insert (e.g. Ctrl+\ for the ASCII FS character). VIM will insert the literal value of the control code into the buffer.
- adwn 8y agoThanks! I had tried Ctrl+V followed by the character from the caret notation (e.g., "Ctrl+V \", instead of "Ctrl+V Ctrl+\"), and that hadn't worked.
- JdeBP 8y agoThe condescension here is supplied by you. Alas, it is based upon inaccuracy. VIM/NeoVIM cannot stop you from entering control characters by pressing [Control] and a key. It is a function of one's terminal or terminal emulator that (aside from employing some fairly unconventional keyboard maps) does not get turned off. You are confusing entering text with typing control characters on a keyboard. One types these control characters with VIM/NeoVIM all of the time. One of them, after all, is ESC, and the others are fairly common as I said. That the control characters are commands, even in insert mode, is a different matter. One such command is the SYN control character, typed on the keyboard of course with the [Control] key as [Control]-V, whose use demonstrates that in VIM/NeoVIM one most definitely can insert control characters as text. The fact that you brought up nano is also somewhat risible. That has a large banner across the bottom of the screen telling you what control characters the editor responds to, which one uses the [Control] key to type on the keyboard. It's staring you right in the face. (FS is even one of the ones on the main menu.) I'll let you discover the equally simple key chord that you can use with nano, which likewise does let you insert control characters as text with the [Control] key. As for Windows, you might want to fire up a console and actually try pressing some [Control]-key chords. For best results, run a console program that follows much the same convention. In TCC, for example, Alt-255 is the literal-next-character convention. But even without such a program, SUB as an end of input indicator, typed on the keyboard as [Control]-Z, is a fairly easy demonstration; and an application-level convention that goes back a long way. * https://jpsoft.com/help/editing.htm https://jpsoft.com/help/editing.htm
- jsjohnst 8y ago> Which you can't type on a keyboard Your keyboard doesn’t have the following keys? ctl-\, ctl-], ctl-^, and ctl-_ Re: “symbolic” escapes, sure, but there’s always: \x1c, \x1d, \x1e, and \x1f Not everything can be a single character, there’s only 26 of those in the English language.
- ben0x539 8y agoMy system seems to terminate the program I'm typing into when I press Control-\. Also, with the default German kez^Hymap, I can't seem to type Control-^.
- simcop2387 8y agoTry Ctrl+V first, if it's not been bound to paste. No idea how a german layout affects things though.
- cryptonector 8y agoYou can type them! E.g., ctrl-^ is record separator. $ echo ^\^]^^^_|od -c 0000000 034 035 036 037 \n 0000005 So, ^\ is FS, ^] is GS, ^^ is RS, and ^_ is US.
- loeg 8y agoIt's probably worth remembering that these combinations are unintuitive and not readily printed on keyboards. Additionally, all text editors can render a CSV correctly; I doubt the same can be said for Unix control characters. I suspect fewer than 1% of computer users has these memorized. For that matter, I suspect fewer than 1% of software developers or sysadmins have these memorized. In contrast, the comma, tab, and return keys are easily located on any keyboard, and CSV delimiters are all so common as to be readily printable by any text editor. Meanwhile, unix escape sequences don't solve the quoting / escape problem of representing strings containing the delimiters anyway.
- icedchai 8y agoGood luck remembering what characters those are and viewing them in most terminals without a hex dump. Remember that CSV is also for human consumption.
- romeisendcoming 8y agoCome up with a convention and stick to it. Bracketed network/host, trailing port. Shoot anyone who does it any other way. Ignore case in network/host...and yes you need to be able to do this. Counting on some other system to do the right thing is silly.
- ninkendo 8y ago> Shoot anyone who does it any other way. > Counting on some other system to do the right thing is silly. Do you see the contradiction in these statements?
- romeisendcoming 8y agoWhy yes, thank you. But without being morbidly explicit joining these phrases without context could put someone in jail...and that is what you did there. My point was to pick a convention and enforce it and (secondly) that using some agency ( I believe postgresql, various APIs, were mentioned throughout) is silly imho. People are going to use strings and parse them. Just make it the same format everywhere.
- deleted 8y ago[deleted]