<commit_before>// Copyright 2016 Documize Inc. <<EMAIL>>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <<EMAIL>>.
//
// https://documize.com

package page

import (
	"fmt"
)

// Numberize calculates numbers for pages, e.g. 1, 1.1, 2.2.1
// for the document's Table of Contents.
func Numberize(pages []Page) {
	var prevPageLevel uint64
	parts := []int{1} // we store 1, 1, 2, and then generate numbering as "1.1.2"

	for i, p := range pages {
		// handle bad data
		if p.Level == 0 {
			p.Level = 1
		}

		if i != 0 {
			// we ...
			if p.Level > prevPageLevel {
				parts = append(parts, 1)
			}

			if p.Level == prevPageLevel {
				parts[len(parts)-1]++
			}

			if p.Level < prevPageLevel {
				end := (prevPageLevel - p.Level)
				if int(end) > len(parts) {
					end = uint64(len(parts))
				}
				parts = parts[0 : len(parts)-int(end)]
				parts[len(parts)-1]++
			}
		}

		// generate numbering for page using parts array
		numbering := ""
		for i, v := range parts {
			dot := ""
			if i != len(parts)-1 {
				dot = "."
			}
			numbering = fmt.Sprintf("%s%d%s", numbering, v, dot)
		}
		pages[i].Numbering = numbering

		// update state
		prevPageLevel = p.Level
	}
}<commit_msg>edge case detection for page numbering logic<commit_after>// Copyright 2016 Documize Inc. <<EMAIL>>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <<EMAIL>>.
//
// https://documize.com

package page

import (
	"fmt"
)

// Numberize calculates numbers for pages, e.g. 1, 1.1, 2.2.1
// for the document's Table of Contents.
func Numberize(pages []Page) {
	var prevPageLevel uint64
	parts := []int{1} // we store 1, 1, 2, and then generate numbering as "1.1.2"

	for i, p := range pages {
		// handle bad data
		if p.Level == 0 {
			p.Level = 1
		}

		if i != 0 {
			// we ...
			if p.Level > prevPageLevel {
				parts = append(parts, 1)
			}

			if p.Level == prevPageLevel {
				parts[len(parts)-1]++
			}

			if p.Level < prevPageLevel {
				end := (prevPageLevel - p.Level)
				if int(end) > len(parts) {
					end = uint64(len(parts))
				}
				parts = parts[0 : len(parts)-int(end)]

				i := len(parts) - 1
				if i < 0 {
					i = 0
				}
				parts[i]++
			}
		}

		// generate numbering for page using parts array
		numbering := ""
		for i, v := range parts {
			dot := ""
			if i != len(parts)-1 {
				dot = "."
			}
			numbering = fmt.Sprintf("%s%d%s", numbering, v, dot)
		}
		pages[i].Numbering = numbering

		// update state
		prevPageLevel = p.Level
	}
}
{'fraction_non_alphanumeric': 0.14967829326109042, 'fraction_numerical': 0.022350152387402643, 'mean_word_length': 2.4958579881656804, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7213153', 'n_tokens_mistral': 1226, 'n_tokens_neox': 1025, 'n_words': 316}