Skip to content

Commit

Permalink
fix: count newline character at the end of the lines
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalinDe committed Jun 21, 2024
1 parent 6f748b2 commit 31df080
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions contracts/evoting/types/ballots.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,30 +333,50 @@ func (s *Subject) MaxEncodedSize() int {

//TODO : optimise by computing max size according to number of choices and maxN
for _, rank := range s.Ranks {
size += len(rank.GetID() + "::")
size += len(rank.GetID())
size += len(rank.ID)

// ':' separators ('id:id:choice')
size += 2

// at most 3 bytes (128) + ',' per choice
size += len(rank.Choices) * 4
size += (len(rank.Choices) * 4)

// line ends w/ '\n'
size += 1
}

for _, selection := range s.Selects {
size += len(selection.GetID() + "::")
size += len(selection.GetID())
size += len(selection.ID)

// ':' separators ('id:id:choice')
size += 2

// 1 bytes (0/1) + ',' per choice
size += len(selection.Choices) * 2
size += (len(selection.Choices) * 2)

// line ends w/ '\n'
size += 1
}

for _, text := range s.Texts {
size += len(text.GetID() + "::")
size += len(text.GetID())
size += len(text.ID)

// ':' separators ('id:id:choice')
size += 2

// at most 4 bytes per character + ',' per answer
maxTextPerAnswer := 4*int(text.MaxLength) + 1
size += maxTextPerAnswer*int(text.MaxN) +
int(math.Max(float64(len(text.Choices)-int(text.MaxN)), 0))
size += (maxTextPerAnswer*int(text.MaxN) +
int(math.Max(float64(len(text.Choices)-int(text.MaxN)), 0)))

// line ends w/ '\n'
size += 1
}

// Last line has 2 '\n'
// additional '\n' on last line
if size != 0 {
size++
}
Expand Down

0 comments on commit 31df080

Please sign in to comment.