Permalink
Browse files

Supporting A* and a* variable width strings

  • Loading branch information...
1 parent fcf8b59 commit 38dac9d3d14c32f373aeca9c4559e8d69ccff4bf @quag committed Jan 4, 2009
Showing with 25 additions and 8 deletions.
  1. +15 −8 Pack.io
  2. +2 −0 PackTest.io
  3. +8 −0 pack-spec.io
View
23 Pack.io
@@ -42,16 +42,20 @@ Pack Format := Object clone do(
instruction := nil
numbers := Sequence clone
format foreach(c,
- if(c isDigit,
- numbers append(c)
+ if(c == "*" at(0),
+ instruction setCount("*")
,
- if(numbers size != 0,
- instruction setCount(numbers asNumber)
- numbers empty
+ if(c isDigit,
+ numbers append(c)
+ ,
+ if(numbers size != 0,
+ instruction setCount(numbers asNumber)
+ numbers empty
+ )
+
+ instruction = instructionProtos at(c asCharacter) clone
+ instructions append(instruction)
)
-
- instruction = instructionProtos at(c asCharacter) clone
- instructions append(instruction)
)
)
@@ -160,6 +164,9 @@ Pack Packer := Object clone do(
)
appendPaddedString := method(string, width, padding,
+ if(width == "*",
+ width = string size
+ )
bytes appendSeq(string exSlice(0, width) alignLeft(width, padding))
)
View
@@ -22,6 +22,7 @@ PackTest := UnitTest clone do(
assertEquals("a ", Pack pack("A2", "a"))
assertEquals("a ", Pack pack("A3", "a"))
assertEquals("b\0", Pack pack("A0Z", "a", "b"))
+ assertEquals("abcd", Pack pack("A*", "abcd"))
)
testNullPaddedString := method(
@@ -30,6 +31,7 @@ PackTest := UnitTest clone do(
assertEquals("a\0", Pack pack("a2", "a"))
assertEquals("a\0\0", Pack pack("a3", "a"))
assertEquals("b\0", Pack pack("a0Z", "a", "b"))
+ assertEquals("abcd", Pack pack("a*", "abcd"))
)
testUnsignedByte := method(
View
@@ -49,6 +49,10 @@ describe(packSpacePadded, "Packing space padded strings",
packSpacePadded("should pad empty strings out to the width",
Pack pack("A4", "") verify(== " ")
)
+
+ packSpacePadded("should use * to mean as wide as is needed",
+ Pack pack("A*", "abcd") verify(== "abcd")
+ )
)
describe(packNullPadded, "Packing null padded strings",
@@ -68,6 +72,10 @@ describe(packNullPadded, "Packing null padded strings",
packNullPadded("should pad empty strings out to the width",
Pack pack("a4", "") verify(== "\0\0\0\0")
)
+
+ packNullPadded("should use * to mean as wide as is needed",
+ Pack pack("a*", "abcd") verify(== "abcd")
+ )
)
describe(packUnsignedByte, "Packing unsigned bytes",

0 comments on commit 38dac9d

Please sign in to comment.