Godot-proposals: Add `String.is_valid_port()` and `String.is_valid_ip()`

Created on 22 Nov 2020  路  15Comments  路  Source: godotengine/godot-proposals

Describe the project you are working on:
Networking test

Describe the problem or limitation you are having in your project:
there doesn't seem to be a way to check if a string is just a valid ip or port

Describe the feature / enhancement and how it helps to overcome the problem or limitation:
Add the following to the string class..

  • is_valid_port
  • is_valid_ip
  • is_valid_ipv4_ip
  • is_valid_ipv6_ip

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

is_valid_port variants would return if a string is a valid port with no ip
is_valid_ip variants would return if a string is a valid ip with no port

If this enhancement will not be used often, can it be worked around with a few lines of script?:
yes but you have no know how to parse ports and ips
also yes I know ipv4 ports are easy to do

Is there a reason why this should be core and not an add-on in the asset library?:
It makes checking these thing easy to do.

core

Most helpful comment

people might not know the valid port range from 1-65535

I don't think this argument is sufficient in itself. If you're making a networked multiplayer game, it's expected that you have some kind of networking know-how.

All 15 comments

Port numbers are identical for IPv4 and IPv6, which means you don't need separate functions for that.

ok I can edit that

There is one for the IP address already, or am I missing something? https://docs.godotengine.org/en/stable/classes/class_string.html#class-string-method-is-valid-ip-address

Not sure if it works with IPv6 though.

Isn't port just a number in a specific range? Why does it need a dedicated method?

@pycbouh there is one but it includes the port
@KoBeWi people might not know the valid port range from 1-65535
also it compliments the ip one

people might not know the valid port range from 1-65535

I don't think this argument is sufficient in itself. If you're making a networked multiplayer game, it's expected that you have some kind of networking know-how.

I mean that's true but what about the it compliments the ip one?
That seems to be a good reason.
We already have a method to check ip and port I think being able to check them separately would be good

Except you can check valid port with one line: if int(port) > 0 and int(port) <= 65535
While checking IP address is more complex, so that method is actually useful.

actually it would be..if port.is_valid_integer() and int(port) > 0 and int(port) <= 65535

but yes the port one is not as useful but it is consistent with suggested IP methods.
it would be useful for quickly checking a port number if you don't want to type..
if port.is_valid_integer() and int(port) > 0 and int(port) <= 65535
a bunch
Also making a function for it is awkward since you have to have the object reference to access it.
and its more of a string function

and again it a complementary suggestions for..

  • is_valid_ip
  • is_valid_ipv4_ip
  • is_valid_ipv6_ip

I'd rather just see an Address or Addr interface added with subtypes for, example, IPv4Addr, and IPv6Addr.

A good example of such an abstractiom is Go's net.Addr.

Are you talking about something like this..

export var host : IPv4Addr

That would be nice but again what about the port wouldn't adding a class like that be the same as adding a string.is_valid_port()?
and if that was the case could there be a port class?

Are you talking about something like this..

export var host : IPv4Addr

That would be nice but again what about the port wouldn't adding a class like that be the same as adding a string.is_valid_port()?
and if that was the case could there be a port class?

Yes. You could have a method that parses a string and another method that converts to string, but also memvers for accessing the individual components, depending on which type of addr it is (for instance for IPAddr, you'd have the ip itself (as a string) and the port (as an int)

The issue is if the player has one line edit or two
one => ip+':'+port
two => ip, port

The issue is if the player has one line edit or two
one => ip+':'+port
two => ip, port

Usability-wise, it's usually considered better to have a single LineEdit for both the address and port, with the port specified using :. If no port is specified, then the default port is used.

Thats true if your screen isn't 128x128 or smaller

Was this page helpful?
0 / 5 - 0 ratings