--- a/TypeConverter.st Wed Oct 07 01:10:12 2015 +0200
+++ b/TypeConverter.st Sun Oct 11 13:29:06 2015 +0200
@@ -297,6 +297,7 @@
#integer
#integerInRange
#integerWithThousandsSeparator
+ #integerCStyle
#fileSize
#frequency
#hexadecimal
@@ -1318,6 +1319,14 @@
self integerToText
!
+integerCStyle
+ "setup the converter to convert from a string to an integer
+ and vice versa. Supports CStyle 0x/0b and 0 prefixes
+ Invalid integers are converted to nil."
+
+ self integerToTextCStyle
+!
+
integerOrNil
"setup the converter to convert from a string to an integer
and vice versa. Invalid numbers are converted to nil."
@@ -1364,6 +1373,48 @@
updateBlock: [:m :a :p | true]
!
+integerToTextCStyle
+ "setup the converter to convert from a string to a integer
+ and vice versa. SUpports C-style integers in the form 0x, 0b 0
+ and also Smalltalk radix integers"
+
+ self
+ getBlock:[:model |
+ |numericValue|
+
+ (numericValue := model value) isInteger ifFalse:[
+ ''
+ ] ifTrue:[
+ numericValue printString
+ ]]
+
+ putBlock:
+ [:model :string |
+
+ |value|
+
+ string isEmptyOrNil ifTrue:[
+ value := 0
+ ] ifFalse:[
+ (string startsWith:'0x') ifTrue:[
+ value := Integer readFromString: (string copyFrom:3) radix:16 onError:[0]
+ ] ifFalse:[
+ (string startsWith:'0b') ifTrue:[
+ value := Integer readFromString: (string copyFrom:3) radix:2 onError:[0]
+ ] ifFalse:[
+ (string startsWith:'0') ifTrue:[
+ value := Integer readFromString: string radix:8 onError:[0]
+ ] ifFalse:[
+ value := Integer readFromString: string onError:[0]
+ ]
+ ]
+ ]
+ ].
+ model value:value]
+
+ updateBlock: [:m :a :p | true]
+!
+
integerToTextMinValue:minVal maxValue:maxVal format:formatStringOrNil
"setup the converter to convert from a string to a number
and vice versa, but clamping the number into the range."