| Bytes | Lang | Time | Link |
|---|---|---|---|
| 121 | C | 250905T173345Z | Toby Spe |
| 200 | C clang | 250905T041624Z | a stone |
| 112 | Janet | 250904T194927Z | Adam |
| 076 | JavaScript ES6 | 201014T070256Z | Arnauld |
| 191 | C# .NET Core | 201014T133447Z | skytomo |
| 079 | Java 8 / Scala polyglot | 201014T081133Z | Kevin Cr |
| 074 | Elixir | 201014T200801Z | pxeger |
| 103 | Python 3 | 201013T235941Z | hyperneu |
| 061 | Perl 5 p | 201014T050150Z | Xcali |
| 086 | AWK | 201014T100751Z | Noodle9 |
| 062 | 05AB1E | 201014T085745Z | Kevin Cr |
| 169 | Haskell | 201014T121238Z | lynn |
| 076 | Charcoal | 201014T111525Z | Neil |
| 059 | Retina 0.8.2 | 201014T001025Z | Neil |
| 073 | Retina 0.8.2 | 201014T000435Z | hyperneu |
C, 121 bytes
u;f(char*s)char*p;strtol(s,&p,0);p+=u=(*p32)==117;p+=*p==p[1];p+=(*p|32)==108;p+=!u*(*p|32)==117;return(*s^48)<10*!*p;}
Since input is encoded in ASCII, we use numeric codepoints so that this works regardless of compilation character set.
How it works:
We use library function strtol() to set p to end of numeric part, then advance it past u and l suffixes and check that it's the end of string. Also test that the string begins with a digit (since strtol() will consume leading whitespace and sign).
int f(const char *s)
{
const char *p;
(void)strtol(s, &p, 0);
int u = 0;
if (tolower(*p) == 'u') ++u,++p;
if (*p == *(p+1)) ++p;
if (tolower(*p) == 'l') ++p;
if (!u && tolower(*p) == 'u') ++p;
return *p == '\0'
&& *s >= '0' && *s <= '9';
}
C (clang), 207 200 bytes
u;l;o;f(char*s){l=*s-48?12:(32|*++s)=='x'?s++,0:14;if(l<14&&!*s)return 0;s+=strspn(s,"ABCDEFabcdef9876543210"+l);for(u=l=0;o=*s&~32;s++)if(o==76&&!l++)s[1]==*s&&s++;else if(u++||o-85)break;return!*s;}
A C solution for a C challenge.
Takes input as a pointer to a string, and returns 1/0 for truthy/falsy.
Explained:
int u, l;
int f(char*s){
/* assign offset for strtok */
l = (*s-48) /* if the first character is not '0': */
? 12 /* use decimal "9876543210" */
: (32|*++s) == 'x' /* else if the first two characters are "0x"/"0X": */
? s++, 0 /* use all characters (l = 0) */
: 14; /* else use octal */
/* handle the case when the string is empty.
* This is also the case when a "0" is input, because of the ++s above
* If this is the case, we are using the octal offset, so check that as well */
if(l < 14 && !*s) return 0;
/* skip past all the digits that we consider valid */
s += strspn(s, "ABCDEFabcdef9876543210" + l);
/* handle the U and L part */
/* u and l are used to count if we have a u or an l yet */
/* o contains the current character, uppercased */
for(u=l=0; o=*s & ~32; s++)
if(o == 76 && !l++) /* if it's an L, and we don't already have an L: */
s[1] == *s && s++;/* if the next character is exactly the same (either ll or LL, then advance the string so it doesn't get counted as 2 l's */
else if(o-85||u) /* else, if we're not a U, or we already have a U, then it's a fail */
break;
else u++; /* otherwise it's a U */
/* if, after removing the digits and counting the L's and U's, the string is empty so *s will be 0 */
/* if it failed, then *s isn't zero */
return !*s;
}
Janet, 112 bytes
|(peg/match~(cmt(*(+(*(+"0X""0x"):h+)(*"0"(any(range"07"))):d+)(+"U""u"'0)(+"LL""ll""L""l"0)(+"U""u"'0)-1),or)$)
Janet’s PEGs are a tiiny bit more verbose than regexes :P
I’m using quite a dirty trick to prevent things like 42Ulu from matching. The pattern (+"U""u"'0) matches an optional U or u, but captures an empty string if the U/u is not present. The entire pattern is then wrapped in a cmt with the or function to check if something was captured.
JavaScript (ES6), 77 76 bytes
Saved 1 byte thanks to @l4m2
s=>/^(0x[\da-f]+|0[0-7]*|[1-9]\d*)(u?l?l?|l?l?u?)$/i.test(s)>/Ll|lL/.test(s)
How?
The first regex is case-insensitive. The only invalid patterns that cannot be filtered out that way are "Ll" and "lL". So we use a 2nd case-sensitive regex to take care of them.
C# (.NET Core), 197 191 bytes
@nwellnhof shaved 6bytes:
using c=System.Console;class P{static void Main(){c.WriteLine(System.Text.RegularExpressions.Regex.IsMatch(c.ReadLine(),@"^(?!.*(Ll|lL))(?i)(0[0-7]*|[1-9]\d*|0x[\da-f]+)(u?l?l?|l?l?u?)$"));}}
Original:
using c=System.Console;using System.Text.RegularExpressions;class P{static void Main(){c.WriteLine(Regex.IsMatch(c.ReadLine(),@"^(?!.*(Ll|lL))(?i)(0[0-7]*|[1-9]\d*|0x[\da-f]+)(u?l?l?|l?l?u?)$"));}}
Java 8 / Scala polyglot, 89 79 bytes
s->s.matches("(?!.*(Ll|lL))(?i)(0[0-7]*|[1-9]\\d*|0x[\\da-f]+)(u?l?l?|l?l?u?)")
-10 bytes thanks to @NahuelFouilleul
Try it online in Java 8.
Try it online in Scala (except with => instead of -> - thanks to @TomerShetah).
Explanation:
s-> // Method with String parameter and boolean return-type
s.matches( // Check whether the input-string matches the regex
"(?!.*(Ll|lL))(?i)(0[0-7]*|[1-9]\\d*|0x[\\da-f]+)(u?l?l?|l?l?u?)")
Regex explanation:
In Java, the String#matches method implicitly adds a leading and trailing ^...$ to match the entire string, so the regex is:
^(?!.*(Ll|lL))(?i)(0[0-7]*|[1-9]\d*|0x[\da-f]+)(u?l?l?|l?l?u?)$
(?! ) # The string should NOT match:
^ .* # Any amount of leading characters
( ) # Followed by:
Ll # "Ll"
|lL # Or "lL"
# (Since the `?!` is a negative lookahead, it acts loose from the
# rest of the regex below)
(?i) # Using case-insensitivity,
^ ( # the string should start with:
0 # A 0
[0-7]* # Followed by zero or more digits in the range [0,7]
| # OR:
[1-9] # A digit in the range [1,9]
\d* # Followed by zero or more digits
| # OR:
0x # A "0x"
[ ]+ # Followed by one or more of:
\d # Digits
a-f # Or letters in the range ['a','f']
)( # And with nothing in between,
)$ # the string should end with:
u? # An optional "u"
l?l? # Followed by no, one, or two "l"
| # OR:
l?l? # No, one, or two "l"
u? # Followed by an optional "u"
Elixir, 74 bytes
&(&1=~~r/^(0[0-7]*|[1-9]\d*|0x[\da-f]+)(u?l?l?|l?l?u?)?$/i&&!(&1=~~r/Ll/))
Python 3, 103 bytes
import re;re.compile("^(0[0-7]*|[1-9]\d*|0[xX][\dA-Fa-f]+)([uU](L|l|LL|ll)?|(L|l|LL|ll)[uU]?)?$").match
just a basic regex, probably very suboptimal
returns a match object for truthy and None for falsy; input may not contain surrounding whitespace
-3 bytes thanks to Digital Trauma (on my Retina answer)
-1 byte thanks to FryAmTheEggman (on my Retina answer)
-3 bytes thanks to pxeger
Perl 5 -p, 65 61 bytes
@NahuelFouilleul shaved 4 bytes
$_=/^(0[0-7]*|0x\p{Hex}+|[1-9]\d*)(u?l?l?|l?l?u?)$/i*!/lL|Ll/
AWK, 86 bytes
{print/^(0[0-7]*|[1-9][0-9]*|0[xX][0-9A-Fa-f]+)([uU](L|l|LL|ll)?|(L|l|LL|ll)[uU]?)?$/}
Simply prints truthy or falsey depending on whether or not the input line matches the regex. Doesn't accept leading or trailing whitespaces.
05AB1E, 63 61 62 bytes
„Uuõª„LLæDl«âDí«JéRʒÅ¿}нõ.;Ðć_ilDć'xQiA6£мÐþQë\7ÝKõQë\þQ}sõÊ*
This isn't too easy without regexes.. :/ Can definitely be golfed a bit more, though.
+1 byte as bug-fix for inputs like "u", "l", "LL", etc. (thanks for noticing @Neil)
Try it online or verify all test cases.
Explanation:
„Uu # Push string "Uu"
õª # Convert it to a list of characters, and append an empty string:
# ["U","u",""]
„LL # Push string "LL"
æ # Take its powerset: ["","L","L","LL"]
Dl # Create a lowercase copy: ["","l","l","ll"]
« # Merge the lists together: ["","L","L","LL","","l","l","ll"]
â # Create all possible pairs of these two lists
Dí # Create a copy with each pair reversed
« # Merge the list of pairs together
J # Join each pair together to a single string
éR # Sort it by length in descending order
We now have the list:
["llu","LLu","llU","LLU","ull","uLL","Ull","ULL","ll","LL","lu","lu","Lu","Lu","lU","lU","LU","LU","ll","LL","ul","ul","uL","uL","Ul","Ul","UL","UL","l","l","L","L","u","u","U","U","l","l","L","L","u","u","U","U","","","",""]
ʒ # Filter this list by:
Å¿ # Where the (implicit) input ends with this string
}н # After the filter: only leave the first (longest) one
õ.; # And remove the first occurrence of this in the (implicit) input
ÐD # Triplicate + duplicate (so there are 4 copies on the stack now)
ć # Extract head; pop and push remainder-string and first character
# separated to the stack
_i # If this first character is a 0:
l # Convert the remainder-string to lowercase
D # Duplicate it †¹
ć # Extract head again
'xQi '# If it's equal to "x":
A # Push the lowercase alphabet
6£ # Only leave the first 6 characters: "abcdef"
м # Remove all those characters from the string
Ð # Triplicate it †²
þ # Only keep all digits in the copy
Q # And check that the two are still the same
# (thus it's a non-negative integer without decimal .0s)
ë # Else:
\ # Discard the remainder-string
7Ý # Push list [0,1,2,3,4,5,6,7]
K # Remove all those digits
õQ # Check what remains is an empty string
ë # Else:
\ # Discard the remainder-string
þ # Only keep all digits
Q # And check that the two are still the same
# (thus it's a non-negative integer without decimal .0s)
}s # After the if-else: Swap the two values on the stack
# (this will get the remaining copy of †² for "0x" cases,
# or the remaining copy of †¹ for other cases)
õÊ # Check that this is NOT an empty string
* # And check that both are truthy
# (after which the result is output implicitly)
Haskell, 169 bytes
import Data.Char
s!p=s>""&&dropWhile p s`elem`do u<-["","u","U"];l<-"":words"L l LL ll";[u++l,l++u]
f('0':x:s)|elem x"xX"=s!isHexDigit|1<2=(x:s)!isOctDigit
f s=s!isDigit
Charcoal, 76 bytes
≔⊟Φ³¬⌕↧θ…0xιη≔✂↧θη⁻LθL⊟Φ⪪”{“↧←;⭆δa”¶⁼ι↧…⮌θLι¹ζ›∧⁺Lζ¬⊖η⬤ζ№E∨×⁸ηχ⍘λφι∨№θLl№θlL
Try it online! Link is to verbose version of code. Explanation:
≔⊟Φ³¬⌕↧θ…0xιη
Find the length of the longest prefix of 0x in the lowercased input.
≔✂↧θη⁻LθL⊟Φ⪪”{“↧←;⭆δa”¶⁼ι↧…⮌θLι¹ζ
Slice off the prefix and also check for a lowercase suffix of ull, ul, llu or lu, and if so then slice that off as well.
›...∨№θLl№θlL
The original input must not contain Ll or lL.
∧⁺Lζ¬⊖η
The sliced string must not be empty unless the prefix was 0.
⬤ζ№E∨×⁸ηχ⍘λφι
Convert the prefix length to 10, 8 or 16 appropriately, then take that many base 62 digits and check that all of the remaining lowercased characters are one of those digits.
Retina 0.8.2, 60 59 bytes
i`^(0[0-7]*|0x[\da-f]+|[1-9]\d*)(u)?(l)?(?-i:\3?)(?(2)|u?)$
Try it online! Link includes test cases. Edit: Saved 1 byte thanks to @FryAmTheEggMan. Explanation:
i`
Match case-insensitively.
^(0[0-7]*|0x[\da-f]+|[1-9]\d*)
Start with either octal, hex or decimal.
(u)?
Optional unsigned specifier.
(l)?
Optional length specifier.
(?-i:\3?)
Optionally repeat the length specifier case sensitively.
(?(2)|u?)$
If no unsigned specifier yet, then another chance for an optional specifier, before the end of the literal.
Retina 0.8.2, 73 bytes
^(0[0-7]*|[1-9]\d*|0[xX][\dA-Fa-f]+)([uU](L|l|LL|ll)?|(L|l|LL|ll)[uU]?)?$
Just the same regex I used. First time using Retina, I'm sure this can be optimized with some Retina golf things!
-3 bytes thanks to Digital Trauma
-1 byte thanks to FryAmTheEggman