<commit_before>﻿using System;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Navigation.Mvc
{
	public static class PagerExtensions
	{
		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, object htmlAttributes = null)
		{
			return Pager(htmlHelper, "<<", "<", ">", ">>", htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, string startRowIndexKey, string maximumRowsKey, string totalRowCountKey, object htmlAttributes = null)
		{
			return Pager(htmlHelper, "<<", "<", ">", ">>", startRowIndexKey, maximumRowsKey, totalRowCountKey, htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, string firstText, string previousText, string nextText, string lastText, object htmlAttributes = null)
		{
			return Pager(htmlHelper, firstText, previousText, nextText, lastText, "startRowIndex", "maximumRows", "totalRowCount", htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, string firstText, string previousText, string nextText, string lastText,
			string startRowIndexKey, string maximumRowsKey, string totalRowCountKey, object htmlAttributes = null)
		{
			return GeneratePager(htmlHelper, null, previousText, nextText, firstText, lastText, startRowIndexKey, maximumRowsKey, totalRowCountKey, htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, int numberOfLinks, object htmlAttributes = null)
		{
			return Pager(htmlHelper, numberOfLinks, "...", "...", htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, int numberOfLinks, string startRowIndexKey, string maximumRowsKey, string totalRowCountKey, object htmlAttributes = null)
		{
			return Pager(htmlHelper, numberOfLinks, "...", "...", startRowIndexKey, maximumRowsKey, totalRowCountKey, htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, int numberOfLinks, string previousText, string nextText, object htmlAttributes = null)
		{
			return Pager(htmlHelper, numberOfLinks, previousText, nextText, "startRowIndex", "maximumRows", "totalRowCount", htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, int numberOfLinks, string previousText, string nextText,
			string startRowIndexKey, string maximumRowsKey, string totalRowCountKey, object htmlAttributes = null)
		{
			return GeneratePager(htmlHelper, numberOfLinks, previousText, nextText, null, null, startRowIndexKey, maximumRowsKey, totalRowCountKey, htmlAttributes);
		}

		private static MvcHtmlString GeneratePager(this HtmlHelper htmlHelper, int? numberOfLinks, 			
			string previousText, string nextText, string firstText, string lastText,
			string startRowIndexKey, string maximumRowsKey, string totalRowCountKey, object htmlAttributes)
		{
			if (StateContext.Data[startRowIndexKey] as int? == null)
				throw new ArgumentException(Resources.InvalidPagerValue, startRowIndexKey);
			if (StateContext.Data[maximumRowsKey] as int? == null)
				throw new ArgumentException(Resources.InvalidPagerValue, maximumRowsKey);
			if (StateContext.Data[totalRowCountKey] as int? == null)
				throw new ArgumentException(Resources.InvalidPagerValue, totalRowCountKey);
			int startRowIndex = (int)StateContext.Data[startRowIndexKey];
			int maximumRows = (int)StateContext.Data[maximumRowsKey];
			int totalRowCount = (int)StateContext.Data[totalRowCountKey];
			StringBuilder pagerBuilder = new StringBuilder();
			if (numberOfLinks.HasValue)
			{
				AddNumericLinks(pagerBuilder, numberOfLinks.Value, startRowIndexKey, maximumRows, totalRowCount, previousText, nextText);
			}
			else
			{
				AddLink(pagerBuilder, firstText, 0, startRowIndexKey, totalRowCount);
				AddLink(pagerBuilder, previousText, startRowIndex - maximumRows, startRowIndexKey, totalRowCount);
				AddLink(pagerBuilder, nextText, startRowIndex + maximumRows, startRowIndexKey, totalRowCount);
				var remainder = totalRowCount % maximumRows;
				AddLink(pagerBuilder, lastText, remainder != 0 ? totalRowCount - remainder : totalRowCount - maximumRows, startRowIndexKey, totalRowCount);
			}
			if (totalRowCount == 0)
				return new MvcHtmlString(string.Empty);
			TagBuilder tagBuilder = new TagBuilder("ul");
			tagBuilder.InnerHtml = pagerBuilder.ToString();
			tagBuilder.MergeAttributes<string, object>(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
			return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
		}

		private static void AddNumericLinks(StringBuilder pagerBuilder, int numberOfLinks,
			string startRowIndexKey, int maximumRows, int totalRowCount,
			string previousText, string nextText)
		{<commit_msg>Moved totalRowCount check to beginning<commit_after>﻿using System;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Navigation.Mvc
{
	public static class PagerExtensions
	{
		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, object htmlAttributes = null)
		{
			return Pager(htmlHelper, "<<", "<", ">", ">>", htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, string startRowIndexKey, string maximumRowsKey, string totalRowCountKey, object htmlAttributes = null)
		{
			return Pager(htmlHelper, "<<", "<", ">", ">>", startRowIndexKey, maximumRowsKey, totalRowCountKey, htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, string firstText, string previousText, string nextText, string lastText, object htmlAttributes = null)
		{
			return Pager(htmlHelper, firstText, previousText, nextText, lastText, "startRowIndex", "maximumRows", "totalRowCount", htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, string firstText, string previousText, string nextText, string lastText,
			string startRowIndexKey, string maximumRowsKey, string totalRowCountKey, object htmlAttributes = null)
		{
			return GeneratePager(htmlHelper, null, previousText, nextText, firstText, lastText, startRowIndexKey, maximumRowsKey, totalRowCountKey, htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, int numberOfLinks, object htmlAttributes = null)
		{
			return Pager(htmlHelper, numberOfLinks, "...", "...", htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, int numberOfLinks, string startRowIndexKey, string maximumRowsKey, string totalRowCountKey, object htmlAttributes = null)
		{
			return Pager(htmlHelper, numberOfLinks, "...", "...", startRowIndexKey, maximumRowsKey, totalRowCountKey, htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, int numberOfLinks, string previousText, string nextText, object htmlAttributes = null)
		{
			return Pager(htmlHelper, numberOfLinks, previousText, nextText, "startRowIndex", "maximumRows", "totalRowCount", htmlAttributes);
		}

		public static MvcHtmlString Pager(this HtmlHelper htmlHelper, int numberOfLinks, string previousText, string nextText,
			string startRowIndexKey, string maximumRowsKey, string totalRowCountKey, object htmlAttributes = null)
		{
			return GeneratePager(htmlHelper, numberOfLinks, previousText, nextText, null, null, startRowIndexKey, maximumRowsKey, totalRowCountKey, htmlAttributes);
		}

		private static MvcHtmlString GeneratePager(this HtmlHelper htmlHelper, int? numberOfLinks, 			
			string previousText, string nextText, string firstText, string lastText,
			string startRowIndexKey, string maximumRowsKey, string totalRowCountKey, object htmlAttributes)
		{
			if (StateContext.Data[startRowIndexKey] as int? == null)
				throw new ArgumentException(Resources.InvalidPagerValue, startRowIndexKey);
			if (StateContext.Data[maximumRowsKey] as int? == null)
				throw new ArgumentException(Resources.InvalidPagerValue, maximumRowsKey);
			if (StateContext.Data[totalRowCountKey] as int? == null)
				throw new ArgumentException(Resources.InvalidPagerValue, totalRowCountKey);
			int startRowIndex = (int)StateContext.Data[startRowIndexKey];
			int maximumRows = (int)StateContext.Data[maximumRowsKey];
			int totalRowCount = (int)StateContext.Data[totalRowCountKey];
			if (totalRowCount == 0)
				return new MvcHtmlString(string.Empty);
			StringBuilder pagerBuilder = new StringBuilder();
			if (numberOfLinks.HasValue)
			{
				AddNumericLinks(pagerBuilder, numberOfLinks.Value, startRowIndexKey, maximumRows, totalRowCount, previousText, nextText);
			}
			else
			{
				AddLink(pagerBuilder, firstText, 0, startRowIndexKey, totalRowCount);
				AddLink(pagerBuilder, previousText, startRowIndex - maximumRows, startRowIndexKey, totalRowCount);
				AddLink(pagerBuilder, nextText, startRowIndex + maximumRows, startRowIndexKey, totalRowCount);
				var remainder = totalRowCount % maximumRows;
				AddLink(pagerBuilder, lastText, remainder != 0 ? totalRowCount - remainder : totalRowCount - maximumRows, startRowIndexKey, totalRowCount);
			}
			TagBuilder tagBuilder = new TagBuilder("ul");
			tagBuilder.InnerHtml = pagerBuilder.ToString();
			tagBuilder.MergeAttributes<string, object>(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
			return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
		}

		private static void AddNumericLinks(StringBuilder pagerBuilder, int numberOfLinks,
			string startRowIndexKey, int maximumRows, int totalRowCount,
			string previousText, string nextText)
		{
{'fraction_non_alphanumeric': 0.08738786279683378, 'fraction_numerical': 0.000633245382585752, 'mean_word_length': 6.195140470766894, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7636902', 'n_tokens_mistral': 2900, 'n_tokens_neox': 2617, 'n_words': 692}