Jump to content

Post the most epic /confusing code you can create


Monkuar

Recommended Posts

Rules:

1) It has to be legit and actually work and have a useful function in society/web world.

2) The code cannot be minimized into one line. It has to have line breaks. (Not necessarily for each new function or short hand property, but so it's partly readable)

3) It has to be written in PHP or Javascript.

 

 

For example, (Not my original code) but I'll start: A time_ago function:

function timeago($tm,$rcs = 0) {
    $cur_tm = time(); 
    $dif = $cur_tm-$tm;
    $pds = array('second','minute','hour','day','week','month','year','decade');
    $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);

    for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);
        $no = floor($no);
        if($no <> 1)
            $pds[$v] .='s';
        $x = sprintf("%d %s ",$no,$pds[$v]);
        if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0))
            $x .= time_ago($_tm);
        return $x;
    }
Your turn! Edited by Monkuar
Link to comment
Share on other sites

Why not?

	/**
	 * <http://tools.ietf.org/html/rfc3492#section-5>
	 */
	const PUNYCODE_DIGITS = 'abcdefghijklmnopqrstuvwxyz0123456789';

	/**
	 * Punycode: Adapt
	 *
	 * <http://tools.ietf.org/html/rfc3492#section-6.1>
	 *
	 * @param int $delta
	 * @param int $numpoints
	 * @param bool $firsttime
	 * @return int
	 */
	protected static function punycodeAdapt($delta, $numpoints, $firsttime) {
		if ($firsttime) {
			$delta = (int)($delta / 700);
		} else {
			$delta >>= 1;
		}
		$delta += (int)($delta / $numpoints);
		for ($k = 0; $delta > 455; $k += 36) {
			$delta = (int)($delta / 35);
		}
		return $k + (int)((36 * $delta) / ($delta + 38));
	}

	/**
	 * Punycode: decode
	 *
	 * <http://tools.ietf.org/html/rfc3492#section-6.2>
	 *
	 * @param string $string
	 * @param string $encoding
	 * @return string
	 */
	public static function punycodeDecode($string, $encoding = "UTF-8") {
		$n = 128;
		$i = 0;
		$bias = 72;

		$output = array();
		$_s = strrpos($string, "-");
		if ($_s !== false) {
			$output += str_split(substr($string, 0, $_s));
			$_s++;
		} else {
			$_s = 0;
		}

		$_slen = strlen($string);
		while ($_s < $_slen) {
			$_olds = $_s;
			$oldi = $i;
			$w = 1;
			for ($k = 36; ; $k += 36) {
				if ($_s >= $_slen) {
					return false;
				}
				$digit = strpos(self::PUNYCODE_DIGITS, strtolower($string[$_s++]));
				if ($digit === false) {
					return false;
				}
				$i += $digit * $w;
				$t = $k - $bias;
				if ($t > 26) {
					$t = 26;
				} else if ($t < 1) {
					$t = 1;
				}
				if ($digit < $t) {
					break;
				}
				$w *= (36 - $t);
			}
			$_len = count($output) + 1;
			$bias = self::punycodeAdapt($i - $oldi, $_len, $oldi == 0);
			$n += (int)($i / $_len);
			$i = ($i % $_len);

			// utf-8 encoding

			// 0xxxxxxx
			if ($n <= 0x3F) {
				$_n = chr($n);
			}
			// 110xxxxx 10xxxxxx
			else if ($n <= 0x7FF) {
				$_n = chr(0xC0 | ($n >> 6 & 0x1F))
					. chr(0x80 | ($n      & 0x3F));
			}
			// 1110xxxx 10xxxxxx 10xxxxxx
			else if ($n <= 0xFFFF) {
				$_n = chr(0xE0 | ($n >> 12 & 0x0F))
					. chr(0x80 | ($n >> 6  & 0x3F))
					. chr(0x80 | ($n       & 0x3F));
			}
			// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
			else if ($n <= 0x1FFFFF) {
				$_n = chr(0xF0 | ($n >> 18 & 0x07))
					. chr(0x80 | ($n >> 12 & 0x3F))
					. chr(0x80 | ($n >> 6  & 0x3F))
					. chr(0x80 | ($n       & 0x3F));
			}
			// fail
			else {
				warning("f.url", ["Invalid Unicode codepoint U+%04X at offset %d '%s'", $n, $_olds, substr($string, $_olds, $_s - $_olds)]);
				$_n = "\xEF\xBF\xBD"; // U+FFFD REPLACEMENT CHARACTER
			}
			array_splice($output, $i, 0, [$_n]);
			$i++;
		}
		return mb_convert_encoding(implode("", $output), $encoding, "UTF-8");
	}

	/**
	 * Punycode: encode
	 *
	 * <http://tools.ietf.org/html/rfc3492#section-6.3>
	 *
	 * @param string $string
	 * @param string $encoding
	 * @return string
	 */
	public static function punycodeEncode($string, $encoding = "UTF-8") {
		$n = 128;
		$delta = 0;
		$bias = 72;
		$h = $b = 0;

		$output = "";
		$_cps = [];
		$_len = mb_strlen($string, $encoding);
		$_c = array_map("ord", str_split(mb_convert_encoding($string, "UTF-8", $encoding)));
		$_ccount = count($_c);
		for ($_i = 0; $_i < $_ccount; ) {
			// utf-8 decoding

			// 0xxxxxxx
			if (($_c[$_i] & 0x80) == 0x00) {
				$_cp = $_c[$_i++];
				$output .= chr($_cp);
				$h++; $b++;
			}
			// 110xxxxx 10xxxxxx
			else if (($_c[$_i] & 0xE0) == 0xC0 && ($_c[$_i + 1] & 0xC0) == 0x80) {
				$_cp = (($_c[$_i++] & 0x1F) << 6) | ($_c[$_i++] & 0x3F);
			}
			// 1110xxxx 10xxxxxx 10xxxxxx
			else if (($_c[$_i] & 0xF0) == 0xE0 && ($_c[$_i + 1] & 0xC0) == 0x80 && ($_c[$_i + 2] & 0xC0) == 0x80) {
				$_cp = (($_c[$_i++] & 0x0F) << 12) | (($_c[$_i++] & 0x3F) << 6) | ($_c[$_i++] & 0x3F);
			}
			// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
			else if (($_c[$_i] & 0xF8) == 0xF0 && ($_c[$_i + 1] & 0xC0) == 0x80 && ($_c[$_i + 2] & 0xC0) == 0x80 && ($_c[$_i + 3] & 0xC0) == 0x80) {
				$_cp = (($_c[$_i++] & 0x07) << 18) | (($_c[$_i++] & 0x3F) << 12) | (($_c[$_i++] & 0x3F) << 6) | ($_c[$_i++] & 0x3F);
			}
			// fail
			else {
				warning("f.url", ["Invalid UTF-8 byte 0x%02X at offset %d", $_c[$_i], $_i]);
				$_cp = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER
			}

			$_cps[] = $_cp;
		}
		$_sortcps = $_cps;
		sort($_sortcps, SORT_NUMERIC);

		if ($b > 0) {
			$output .= "-";
		}

		while ($h < $_len) {
			do {
				$m = array_shift($_sortcps);
			} while ($m < $n);
			$delta += ($m - $n) * ($h + 1);
			$n = $m;
			foreach ($_cps as $c) {
				if ($c < $n) {
					$delta++;
				} else if ($c == $n) {
					$q = $delta;
					for ($k = 36; ; $k += 36) {
						$t = $k - $bias;
						if ($t > 26) {
							$t = 26;
						} else if ($t < 1) {
							$t = 1;
						}
						if ($q < $t) {
							break;
						}
						$output .= substr(self::PUNYCODE_DIGITS, $t + (($q - $t) % (36 - $t)), 1);
						$q = (int)(($q - $t) / (36 - $t));
					}
					$output .= substr(self::PUNYCODE_DIGITS, $q, 1);
					$bias = self::punycodeAdapt($delta, $h + 1, $h == $b);
					$delta = 0;
					$h++;
				}
			}
			$delta++;
			$n++;
		}
		return $output;
	}
Code has to do with IDNs (international domain names) and converting them to/from ASCII representation using an algorithm called Punycode. Which is distressingly complicated. Edited by requinix
Link to comment
Share on other sites

A number system with little-endian ordering is used which allows variable-length codes without separate delimiters: a digit lower than a threshold value marks that it is the most-significant digit, hence the end of the number. The threshold value depends on the position in the number and also on previous insertions, to increase efficiency. Correspondingly the weights of the digits vary.

 

 

In this case a number system with 36 digits is used, with the case-insensitive 'a' through 'z' equal to the numbers 0 through 25, and '0' through '9' equal to 26 through 35. Thus "kva", corresponds to "10 21 0".

 

  :

 

Nice one Req, I thought you were going to do something with the number bytes format code you crafted earlier though. Still impressive though, love this.

Edited by Monkuar
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.