0

I was recently implementing the following algorithm (written for clarity; there are many ways to write it more compactly):

tmp = str.length % blocksize
if (tmp == 0)
  tmp = blocksize
tmp = blocksize - tmp
str.append(padchar * tmp)

It ensues the string str is a multiple of blocksize, padding with padchar as necessary. I've found myself implementing this often enough, that today I implemented it yet again, and realized that it must be common enough to have a proper name or be in some common libraries, yet I've never seen or heard of it. This example is left-justified, but I could imagine a similar right-variant. My attempts at googling "chunk justification" or "block justification" turned up nothing. It isn't standard left or right justification, as all api's for those that I've seen are fixed length, whereas this is semi-variable length.

So, my two questions:

  1. Does this have a more formal name?
  2. Is this algorithm implemented in any language standard library or common utilities?

(note that although I've used strings in this question, the algorithm works just as well for, and I'm just as interested in answers for generic lists/arrays)

Example output:

> thisalgo(str="hey!", blocksize=5, padchar='0')
"hey!0" 
> thisalgo(str="hello", blocksize=5, padchar='0')
"hello" 
> thisalgo(str="hello!", blocksize=5, padchar='0')
"hello!0000" 

byteit101
  • 3,236
  • 1
  • 16
  • 24
  • I suggest you look into [StringUtils.rightPad](https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#rightPad(java.lang.String,%20int)) – molamk Feb 28 '19 at 05:25
  • My reading of that doc looks like a typical left/right justify algorithm, and doesn't seem to mention the "block" or "chunk" behavior I'm inquiring about. – byteit101 Feb 28 '19 at 05:29
  • You can set the padding size to be `Math.ceil(str.length() / blocksize) * blocksize`, which will give you similar results – molamk Feb 28 '19 at 05:32
  • You can write this `str.append(padchar * (blocksize - 1 - (str.length + blocksize - 1) % blocksize))` – Matt Timmermans Feb 28 '19 at 13:06
  • I would probably say "Block-Aligned" or "Block-Padded", since justification is usually used in conjunction with text/string processing, and "chunk" seems less formal than "block". – Dillon Davis Mar 02 '19 at 02:47

1 Answers1

2

Not sure about a single name, but there are two operations involved:

I would call the combined operation "Pad string to next multiple", which is quite intuitive, but not a 'proper' name.

BTW, this combined operation is used extensively in cryptography, where it is simply called 'padding'.

Lior Kogan
  • 18,061
  • 4
  • 49
  • 79
  • 1
    Why `aString.padToNextMultipleOf( blockSize )` wouldn't be a proper name??? It seems to perfectly express the intent. – aka.nice Mar 02 '19 at 17:29
  • @aka.nice. I'm not sure what exactly the OP means by 'proper name'. I assume the OP means standard / formal name. – Lior Kogan Mar 02 '19 at 17:36
  • Had entirely forgotten about crypto padding, which perfectly answers the second part! @LiorKogan yes that is what I meant. Edited question to clarify. – byteit101 Mar 02 '19 at 20:15