on master help sign in
Fix intToAscii for multi-character names
431965cEvan Czaplicki
src/Generate/JavaScript/Name.hs+20−26
@@ -231,47 +231,41 @@ intToAscii n =
231 S.pack [toByte n]
231 S.pack [toByte n]
232
232
233 else
233 else
234− intToAsciiHelp (numStartBytes * numInnerBytes) allBadFields (n - 53)
234+ intToAsciiHelp 2 (numStartBytes * numInnerBytes) allBadFields (n - 53)
235
235
236
236
237−intToAsciiHelp :: Int -> [BadFields] -> Int -> S.ShortByteString
237+intToAsciiHelp :: Int -> Int -> [BadFields] -> Int -> S.ShortByteString
238−intToAsciiHelp blockSize badFields n =
238+intToAsciiHelp width blockSize badFields n =
239 case badFields of
239 case badFields of
240 [] ->
240 [] ->
241 if n < blockSize then
241 if n < blockSize then
242− unsafeIntToAscii n
242+ unsafeIntToAscii width [] n
243 else
243 else
244− intToAsciiHelp (blockSize * numInnerBytes) [] (n - blockSize)
244+ intToAsciiHelp (width + 1) (blockSize * numInnerBytes) [] (n - blockSize)
245
245
246 BadFields renamings : biggerBadFields ->
246 BadFields renamings : biggerBadFields ->
247 let availableSize = blockSize - Map.size renamings in
247 let availableSize = blockSize - Map.size renamings in
248 if n < availableSize then
248 if n < availableSize then
249− let name = unsafeIntToAscii n in
249+ let name = unsafeIntToAscii width [] n in
250 Map.findWithDefault name name renamings
250 Map.findWithDefault name name renamings
251 else
251 else
252− intToAsciiHelp (blockSize * numInnerBytes) biggerBadFields (n - availableSize)
252+ intToAsciiHelp (width + 1) (blockSize * numInnerBytes) biggerBadFields (n - availableSize)
253
253
254
254
255
255
256-- UNSAFE INT TO ASCII
256-- UNSAFE INT TO ASCII
257
257
258
258
259−unsafeIntToAscii :: Int -> S.ShortByteString
259+unsafeIntToAscii :: Int -> [Word8] -> Int -> S.ShortByteString
260−unsafeIntToAscii n =
260+unsafeIntToAscii width bytes n =
261− let
261+ if width <= 1 then
262− (quotient, remainder) =
262+ S.pack (toByte n : bytes)
263− quotRem n numStartBytes
264− in
265− S.pack $ toByte remainder : unsafeIntToAsciiHelp quotient
266−
267−
268−unsafeIntToAsciiHelp :: Int -> [Word8]
269−unsafeIntToAsciiHelp n =
270− if n == 0 then
271− []
272 else
263 else
264+ let
265+ (quotient, remainder) =
273− let (quotient, remainder) = quotRem n numInnerBytes in
266+ quotRem n numInnerBytes
267+ in
274− toByte remainder : unsafeIntToAsciiHelp quotient
268+ unsafeIntToAscii (width - 1) (toByte remainder : bytes) quotient
275
269
276
270
277
271
@@ -322,12 +316,12 @@ allBadFields =
322addRenaming :: N.Name -> Maybe BadFields -> BadFields
316addRenaming :: N.Name -> Maybe BadFields -> BadFields
323addRenaming keyword maybeBadFields =
317addRenaming keyword maybeBadFields =
324 let
318 let
325− maxName =
319+ width = N.length keyword
326− numStartBytes * numInnerBytes ^ (N.length keyword - 1) - 1
320+ maxName = numStartBytes * numInnerBytes ^ (width - 1) - 1
327 in
321 in
328 case maybeBadFields of
322 case maybeBadFields of
329 Nothing ->
323 Nothing ->
330− BadFields $ Map.singleton (N.toShort keyword) (unsafeIntToAscii maxName)
324+ BadFields $ Map.singleton (N.toShort keyword) (unsafeIntToAscii width [] maxName)
331
325
332 Just (BadFields renamings) ->
326 Just (BadFields renamings) ->
333− BadFields $ Map.insert (N.toShort keyword) (unsafeIntToAscii (maxName - Map.size renamings)) renamings
327+ BadFields $ Map.insert (N.toShort keyword) (unsafeIntToAscii width [] (maxName - Map.size renamings)) renamings