text:
/* @internal */
namespace ts.Completions.StringCompletions {
    export function getStringLiteralCompletions(sourceFile: SourceFile, position: number, contextToken: Node | undefined, checker: TypeChecker, options: CompilerOptions, host: LanguageServiceHost, log: Log, preferences: UserPreferences): CompletionInfo | undefined {
        if (isInReferenceComment(sourceFile, position)) {
            const entries = getTripleSlashReferenceCompletion(sourceFile, position, options, host);
            return entries && convertPathCompletions(entries);
        }
        if (isInString(sourceFile, position, contextToken)) {
            if (!contextToken || !isStringLiteralLike(contextToken)) return undefined;
            const entries = getStringLiteralCompletionEntries(sourceFile, contextToken, position, checker, options, host);
            return convertStringLiteralCompletions(entries, contextToken, sourceFile, checker, log, preferences);
        }
    }

    function convertStringLiteralCompletions(completion: StringLiteralCompletion | undefined, contextToken: Node, sourceFile: SourceFile, checker: TypeChecker, log: Log, preferences: UserPreferences): CompletionInfo | undefined {
        if (completion === undefined) {
            return undefined;
        }
        switch (completion.kind) {
            case StringLiteralCompletionKind.Paths:
                return convertPathCompletions(completion.paths);
            case StringLiteralCompletionKind.Properties: {
                const entries: CompletionEntry[] = [];
                getCompletionEntriesFromSymbols(
                    completion.symbols,
                    entries,
                    contextToken,
                    sourceFile,
                    sourceFile,
                    checker,
                    ScriptTarget.ESNext,
                    log,
                    CompletionKind.String,
                    preferences
                ); // Target will not be used, so arbitrary
                return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: completion.hasIndexSignature, entries };
            }
            case StringLiteralCompletionKind.Types: {
                const entries = completion.types.map(type => ({
                    name: type.value,
                    kindModifiers: ScriptElementKindModifier.none,
                    kind: ScriptElementKind.string,
                    sortText: "0",
                    replacementSpan: getReplacementSpanForContextToken(contextToken)
                }));
                return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: completion.isNewIdentifier, entries };
            }
            default:
                return Debug.assertNever(completion);
        }
    }

    export function getStringLiteralCompletionDetails(name: string, sourceFile: SourceFile, position: number, contextToken: Node | undefined, checker: TypeChecker, options: CompilerOptions, host: LanguageServiceHost, cancellationToken: CancellationToken) {
        if (!contextToken || !isStringLiteralLike(contextToken)) return undefined;
        const completions = getStringLiteralCompletionEntries(sourceFile, contextToken, position, checker, options, host);
        return completions && stringLiteralCompletionDetails(name, contextToken, completions, sourceFile, checker, cancellationToken);
    }

    function stringLiteralCompletionDetails(name: string, location: Node, completion: StringLiteralCompletion, sourceFile: SourceFile, checker: TypeChecker, cancellationToken: CancellationToken): CompletionEntryDetails | undefined {
        switch (completion.kind) {
            case StringLiteralCompletionKind.Paths: {
                const match = find(completion.paths, p => p.name === name);
                return match && createCompletionDetails(name, kindModifiersFromExtension(match.extension), match.kind, [textPart(name)]);
            }
            case StringLiteralCompletionKind.Properties: {
                const match = find(completion.symbols, s => s.name === name);
                return match && createCompletionDetailsForSymbol(match, checker, sourceFile, location, cancellationToken);
            }
            case StringLiteralCompletionKind.Types:
                return find(completion.types, t => t.value === name) ? createCompletionDetails(name, ScriptElementKindModifier.none, ScriptElementKind.typeElement, [textPart(name)]) : undefined;
            default:
                return Debug.assertNever(completion);
        }
    }

    function convertPathCompletions(pathCompletions: readonly PathCompletion[]): CompletionInfo {
        const isGlobalCompletion = false; // We don't want the editor to offer any other completions, such as snippets, inside a comment.
        const isNewIdentifierLocation = true; // The user may type in a path that doesn't yet exist, creating a "new identifier" with respect to the collection of identifiers the server is aware of.
        const entries = pathCompletions.map(({ name, kind, span, extension }): CompletionEntry =>
            ({ name, kind, kindModifiers: kindModifiersFromExtension(extension), sortText: SortText.LocationPriority, replacementSpan: span }));
        return { isGlobalCompletion, isMemberCompletion: false, isNewIdentifierLocation, entries };
    }
    function kindModifiersFromExtension(extension: Extension | undefined): ScriptElementKindModifier {
        switch (extension) {
            case Extension.Dts: return ScriptElementKindModifier.dtsModifier;
            case Extension.Js: return ScriptElementKindModifier.jsModifier;
            case Extension.Json: return ScriptElementKindModifier.jsonModifier;
            case Extension.Jsx: return ScriptElementKindModifier.jsxModifier;
            case Extension.Ts: return ScriptElementKindModifier.tsModifier;
            case Extension.Tsx: return ScriptElementKindModifier.tsxModifier;
            case Extension.TsBuildInfo: return Debug.fail(`Extension ${Extension.TsBuildInfo} is unsupported.`);
            case undefined: return ScriptElementKindModifier.none;
            default:
                return Debug.assertNever(extension);
        }
    }

    const enum StringLiteralCompletionKind { Paths, Properties, Types }
    interface StringLiteralCompletionsFromProperties {
        readonly kind: StringLiteralCompletionKind.Properties;
        readonly symbols: readonly Symbol[];
        readonly hasIndexSignature: boolean;
    }
    interface StringLiteralCompletionsFromTypes {
        readonly kind: StringLiteralCompletionKind.Types;
        readonly types: readonly StringLiteralType[];
        readonly isNewIdentifier: boolean;
    }
    type StringLiteralCompletion = { readonly kind: StringLiteralCompletionKind.Paths, readonly paths: readonly PathCompletion[] } | StringLiteralCompletionsFromProperties | StringLiteralCompletionsFromTypes;
    function getStringLiteralCompletionEntries(sourceFile: SourceFile, node: StringLiteralLike, position: number, typeChecker: TypeChecker, compilerOptions: CompilerOptions, host: LanguageServiceHost): StringLiteralCompletion | undefined {
        const { parent } = node;
        switch (parent.kind) {
            case SyntaxKind.LiteralType:
                switch (parent.parent.kind) {
                    case SyntaxKind.TypeReference:
                        return { kind: StringLiteralCompletionKind.Types, types: getStringLiteralTypes(typeChecker.getTypeArgumentConstraint(parent as LiteralTypeNode)), isNewIdentifier: false };
                    case SyntaxKind.IndexedAccessType:
                        // Get all apparent property names
                        // i.e. interface Foo {
                        //          foo: string;
                        //          bar: string;
                        //      }
                        //      let x: Foo["/*completion position*/"]
                        return stringLiteralCompletionsFromProperties(typeChecker.getTypeFromTypeNode((parent.parent as IndexedAccessTypeNode).objectType));
                    case SyntaxKind.ImportType:
                        return { kind: StringLiteralCompletionKind.Paths, paths: getStringLiteralCompletionsFromModuleNames(sourceFile, node, compilerOptions, host, typeChecker) };
                    case SyntaxKind.UnionType: {
                        if (!isTypeReferenceNode(parent.parent.parent)) return undefined;
                        const alreadyUsedTypes = getAlreadyUsedTypesInStringLiteralUnion(parent.parent as UnionTypeNode, parent as LiteralTypeNode);
                        const types = getStringLiteralTypes(typeChecker.getTypeArgumentConstraint(parent.parent as UnionTypeNode)).filter(t => !contains(alreadyUsedTypes, t.value));
                        return { kind: StringLiteralCompletionKind.Types, types, isNewIdentifier: false };
                    }
                    default:
                        return undefined;
                }

            case SyntaxKind.PropertyAssignment:
                if (isObjectLiteralExpression(parent.parent) && (<PropertyAssignment>parent).name === node) {
                    // Get quoted name of properties of the object literal expression
                    // i.e. interface ConfigFiles {
                    //          'jspm:dev': string
                    //      }
                    //      let files: ConfigFiles = {
                    //          '/*completion position*/'
                    //      }
                    //
                    //      function foo(c: ConfigFiles) {}
                    //      foo({
                    //          '/*completion position*/'
                    //      });
                    return stringLiteralCompletionsFromProperties(typeChecker.getContextualType(parent.parent));
                }
                return fromContextualType();

            case SyntaxKind.ElementAccessExpression: {
                const { expression, argumentExpression } = parent as ElementAccessExpression;
                if (node === argumentExpression) {
                    // Get all names of properties on the expression
                    // i.e. interface A {
                    //      'prop1': string
                    // }
                    // let a: A;
                    // a['/*completion position*/']
                    return stringLiteralCompletionsFromProperties(typeChecker.getTypeAtLocation(expression));
                }
                return undefined;
            }

            case SyntaxKind.CallExpression:
            case SyntaxKind.NewExpression:
                if (!isRequireCall(parent, /*checkArgumentIsStringLiteralLike*/ false) && !isImportCall(parent)) {
                    const argumentInfo = SignatureHelp.getArgumentInfoForCompletions(node, position, sourceFile);
                    // Get string literal completions from specialized signatures of the target
                    // i.e. declare function f(a: 'A');
                    // f("/*completion position*/")
                    return argumentInfo ? getStringLiteralCompletionsFromSignature(argumentInfo, typeChecker) : fromContextualType();
                }
                // falls through (is `require("")` or `import("")`)

            case SyntaxKind.ImportDeclaration:
            case SyntaxKind.ExportDeclaration:
            case SyntaxKind.ExternalModuleReference:
                // Get all known external module names or complete a path to a module
                // i.e. import * as ns from "/*completion position*/";
                //      var y = import("/*completion position*/");
                //      import x = require("/*completion position*/");
                //      var y = require("/*completion position*/");
                //      export * from "/*completion position*/";
                return { kind: StringLiteralCompletionKind.Paths, paths: getStringLiteralCompletionsFromModuleNames(sourceFile, node, compilerOptions, host, typeChecker) };

            default:
                return fromContextualType();
        }

        function fromContextualType(): StringLiteralCompletion {
            // Get completion for string literal from string literal type
            // i.e. var x: "hi" | "hello" = "/*completion position*/"
            return { kind: StringLiteralCompletionKind.Types, types: getStringLiteralTypes(getContextualTypeFromParent(node, typeChecker)), isNewIdentifier: false };
        }
    }

    function getAlreadyUsedTypesInStringLiteralUnion(union: UnionTypeNode, current: LiteralTypeNode): readonly string[] {
        return mapDefined(union.types, type =>
            type !== current && isLiteralTypeNode(type) && isStringLiteral(type.literal) ? type.literal.text : undefined);
    }

    function getStringLiteralCompletionsFromSignature(argumentInfo: SignatureHelp.ArgumentInfoForCompletions, checker: TypeChecker): StringLiteralCompletionsFromTypes {
        let isNewIdentifier = false;

        const uniques = createMap<true>();
        const candidates: Signature[] = [];
        checker.getResolvedSignature(argumentInfo.invocation, candidates, argumentInfo.argumentCount);
        const types = flatMap(candidates, candidate => {
            if (!signatureHasRestParameter(candidate) && argumentInfo.argumentCount > candidate.parameters.length) return;
            const type = checker.getParameterType(candidate, argumentInfo.argumentIndex);
            isNewIdentifier = isNewIdentifier || !!(type.flags & TypeFlags.String);
            return getStringLiteralTypes(type, uniques);
        });

        return { kind: StringLiteralCompletionKind.Types, types, isNewIdentifier };
    }

    function stringLiteralCompletionsFromProperties(type: Type | undefined): StringLiteralCompletionsFromProperties | undefined {
        return type && {
            kind: StringLiteralCompletionKind.Properties,
            symbols: filter(type.getApparentProperties(), prop => !(prop.valueDeclaration && isPrivateIdentifierPropertyDeclaration(prop.valueDeclaration))),
            hasIndexSignature: hasIndexSignature(type)
        };
    }

    function getStringLiteralTypes(type: Type | undefined, uniques = createMap<true>()): readonly StringLiteralType[] {
        if (!type) return emptyArray;
        type = skipConstraint(type);
        return type.isUnion() ? flatMap(type.types, t => getStringLiteralTypes(t, uniques)) :
            type.isStringLiteral() && !(type.flags & TypeFlags.EnumLiteral) && addToSeen(uniques, type.value) ? [type] : emptyArray;
    }

    interface NameAndKind {
        readonly name: string;
        readonly kind: ScriptElementKind.scriptElement | ScriptElementKind.directory | ScriptElementKind.externalModuleName;
        readonly extension: Extension | undefined;
    }
    interface PathCompletion extends NameAndKind {
        readonly span: TextSpan | undefined;
    }

    function nameAndKind(name: string, kind: NameAndKind["kind"], extension: Extension | undefined): NameAndKind {
        return { name, kind, extension };
    }
    function directoryResult(name: string): NameAndKind {
        return nameAndKind(name, ScriptElementKind.directory, /*extension*/ undefined);
    }

    function addReplacementSpans(text: string, textStart: number, names: readonly NameAndKind[]): readonly PathCompletion[] {
        const span = getDirectoryFragmentTextSpan(text, textStart);
        return names.map(({ name, kind, extension }): PathCompletion => ({ name, kind, extension, span }));
    }

    function getStringLiteralCompletionsFromModuleNames(sourceFile: SourceFile, node: LiteralExpression, compilerOptions: CompilerOptions, host: LanguageServiceHost, typeChecker: TypeChecker): readonly PathCompletion[] {
        return addReplacementSpans(node.text, node.getStart(sourceFile) + 1, getStringLiteralCompletionsFromModuleNamesWorker(sourceFile, node, compilerOptions, host, typeChecker));
    }

    function getStringLiteralCompletionsFromModuleNamesWorker(sourceFile: SourceFile, node: LiteralExpression, compilerOptions: CompilerOptions, host: LanguageServiceHost, typeChecker: TypeChecker): readonly NameAndKind[] {
        const literalValue = normalizeSlashes(node.text);

        const scriptPath = sourceFile.path;
        const scriptDirectory = getDirectoryPath(scriptPath);

        return isPathRelativeToScript(literalValue) || !compilerOptions.baseUrl && (isRootedDiskPath(literalValue) || isUrl(literalValue))
            ? getCompletionEntriesForRelativeModules(literalValue, scriptDirectory, compilerOptions, host, scriptPath)
            : getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, compilerOptions, host, typeChecker);
    }

    interface ExtensionOptions {
        readonly extensions: readonly Extension[];
        readonly includeExtensions: boolean;
    }
    function getExtensionOptions(compilerOptions: CompilerOptions, includeExtensions = false): ExtensionOptions {
        return { extensions: getSupportedExtensionsForModuleResolution(compilerOptions), includeExtensions };
    }
    function getCompletionEntriesForRelativeModules(literalValue: string, scriptDirectory: string, compilerOptions: CompilerOptions, host: LanguageServiceHost, scriptPath: Path) {
        const extensionOptions = getExtensionOptions(compilerOptions);
        if (compilerOptions.rootDirs) {
            return getCompletionEntriesForDirectoryFragmentWithRootDirs(
                compilerOptions.rootDirs, literalValue, scriptDirectory, extensionOptions, compilerOptions, host, scriptPath);
        }
        else {
            return getCompletionEntriesForDirectoryFragment(literalValue, scriptDirectory, extensionOptions, host, scriptPath);
        }
    }

    function getSupportedExtensionsForModuleResolution(compilerOptions: CompilerOptions): readonly Extension[] {
        const extensions = getSupportedExtensions(compilerOptions);
        return compilerOptions.resolveJsonModule && getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs ?
            extensions.concat(Extension.Json) :
            extensions;
    }

    /**
     * Takes a script path and returns paths for all potential folders that could be merged with its
     * containing folder via the "rootDirs" compiler option
     */
    function getBaseDirectoriesFromRootDirs(rootDirs: string[], basePath: string, scriptDirectory: string, ignoreCase: boolean): readonly string[] {
        // Make all paths absolute/normalized if they are not already
        rootDirs = rootDirs.map(rootDirectory => normalizePath(isRootedDiskPath(rootDirectory) ? rootDirectory : combinePaths(basePath, rootDirectory)));

        // Determine the path to the directory containing the script relative to the root directory it is contained within
        const relativeDirectory = firstDefined(rootDirs, rootDirectory =>
            containsPath(rootDirectory, scriptDirectory, basePath, ignoreCase) ? scriptDirectory.substr(rootDirectory.length) : undefined)!; // TODO: GH#18217

        // Now find a path for each potential directory that is to be merged with the one containing the script
        return deduplicate<string>(
            [...rootDirs.map(rootDirectory => combinePaths(rootDirectory, relativeDirectory)), scriptDirectory],
            equateStringsCaseSensitive,
            compareStringsCaseSensitive);
    }

    function getCompletionEntriesForDirectoryFragmentWithRootDirs(rootDirs: string[], fragment: string, scriptDirectory: string, extensionOptions: ExtensionOptions, compilerOptions: CompilerOptions, host: LanguageServiceHost, exclude: string): readonly NameAndKind[] {
        const basePath = compilerOptions.project || host.getCurrentDirectory();
        const ignoreCase = !(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames());
        const baseDirectories = getBaseDirectoriesFromRootDirs(rootDirs, basePath, scriptDirectory, ignoreCase);
        return flatMap(baseDirectories, baseDirectory => getCompletionEntriesForDirectoryFragment(fragment, baseDirectory, extensionOptions, host, exclude));
    }

    /**
     * Given a path ending at a directory, gets the completions for the path, and filters for those entries containing the basename.
     */
    function getCompletionEntriesForDirectoryFragment(fragment: string, scriptPath: string, { extensions, includeExtensions }: ExtensionOptions, host: LanguageServiceHost, exclude?: string, result: NameAndKind[] = []): NameAndKind[] {
        if (fragment === undefined) {
            fragment = "";
        }

        fragment = normalizeSlashes(fragment);

        /**
         * Remove the basename from the path. Note that we don't use the basename to filter completions;
         * the client is responsible for refining completions.
         */
        if (!hasTrailingDirectorySeparator(fragment)) {
            fragment = getDirectoryPath(fragment);
        }

        if (fragment === "") {
            fragment = "." + directorySeparator;
        }

        fragment = ensureTrailingDirectorySeparator(fragment);

        // const absolutePath = normalizeAndPreserveTrailingSlash(isRootedDiskPath(fragment) ? fragment : combinePaths(scriptPath, fragment)); // TODO(rbuckton): should use resolvePaths
        const absolutePath = resolvePath(scriptPath, fragment);
        const baseDirectory = hasTrailingDirectorySeparator(absolutePath) ? absolutePath : getDirectoryPath(absolutePath);

        const ignoreCase = !(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames());
        if (!tryDirectoryExists(host, baseDirectory)) return result;

        // Enumerate the available files if possible
        const files = tryReadDirectory(host, baseDirectory, extensions, /*exclude*/ undefined, /*include*/ ["./*"]);

        if (files) {
            /**
             * Multiple file entries might map to the same truncated name once we remove extensions
             * (happens iff includeExtensions === false)so we use a set-like data structure. Eg:
             *
             * both foo.ts and foo.tsx become foo
             */
            const foundFiles = createMap<Extension | undefined>(); // maps file to its extension
            for (let filePath of files) {
                filePath = normalizePath(filePath);
                if (exclude && comparePaths(filePath, exclude, scriptPath, ignoreCase) === Comparison.EqualTo) {
                    continue;
                }

                const foundFileName = includeExtensions || fileExtensionIs(filePath, Extension.Json) ? getBaseFileName(filePath) : removeFileExtension(getBaseFileName(filePath));
                foundFiles.set(foundFileName, tryGetExtensionFromPath(filePath));
            }

            foundFiles.forEach((ext, foundFile) => {
                result.push(nameAndKind(foundFile, ScriptElementKind.scriptElement, ext));
            });
        }

        // If possible, get folder completion as well
        const directories = tryGetDirectories(host, baseDirectory);

        if (directories) {
            for (const directory of directories) {
                const directoryName = getBaseFileName(normalizePath(directory));
                if (directoryName !== "@types") {
                    result.push(directoryResult(directoryName));
                }
            }
        }

        // check for a version redirect
        const packageJsonPath = findPackageJson(baseDirectory, host);
        if (packageJsonPath) {
            const packageJson = readJson(packageJsonPath, host as { readFile: (filename: string) => string | undefined });
            const typesVersions = (packageJson as any).typesVersions;
            if (typeof typesVersions === "object") {
                const versionResult = getPackageJsonTypesVersionsPaths(typesVersions);
                const versionPaths = versionResult && versionResult.paths;
                const rest = absolutePath.slice(ensureTrailingDirectorySeparator(baseDirectory).length);
                if (versionPaths) {
                    addCompletionEntriesFromPaths(result, rest, baseDirectory, extensions, versionPaths, host);
                }
            }
        }

        return result;
    }

    function addCompletionEntriesFromPaths(result: NameAndKind[], fragment: string, baseDirectory: string, fileExtensions: readonly string[], paths: MapLike<string[]>, host: LanguageServiceHost) {
        for (const path in paths) {
            if (!hasProperty(paths, path)) continue;
            const patterns = paths[path];
            if (patterns) {
                for (const { name, kind, extension } of getCompletionsForPathMapping(path, patterns, fragment, baseDirectory, fileExtensions, host)) {
                    // Path mappings may provide a duplicate way to get to something we've already added, so don't add again.
                    if (!result.some(entry => entry.name === name)) {
                        result.push(nameAndKind(name, kind, extension));
                    }
                }
            }
        }
    }

    /**
     * Check all of the declared modules and those in node modules. Possible sources of modules:
     *      Modules that are found by the type checker
     *      Modules found relative to "baseUrl" compliler options (including patterns from "paths" compiler option)
     *      Modules from node_modules (i.e. those listed in package.json)
     *          This includes all files that are found in node_modules/moduleName/ with acceptable file extensions
     */
    function getCompletionEntriesForNonRelativeModules(fragment: string, scriptPath: string, compilerOptions: CompilerOptions, host: LanguageServiceHost, typeChecker: TypeChecker): readonly NameAndKind[] {
        const { baseUrl, paths } = compilerOptions;

        const result: NameAndKind[] = [];

        const extensionOptions = getExtensionOptions(compilerOptions);
        if (baseUrl) {
            const projectDir = compilerOptions.project || host.getCurrentDirectory();
            const absolute = normalizePath(combinePaths(projectDir, baseUrl));
            getCompletionEntriesForDirectoryFragment(fragment, absolute, extensionOptions, host, /*exclude*/ undefined, result);
            if (paths) {
                addCompletionEntriesFromPaths(result, fragment, absolute, extensionOptions.extensions, paths, host);
            }
        }

        const fragmentDirectory = getFragmentDirectory(fragment);
        for (const ambientName of getAmbientModuleCompletions(fragment, fragmentDirectory, typeChecker)) {
            result.push(nameAndKind(ambientName, ScriptElementKind.externalModuleName, /*extension*/ undefined));
        }

        getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, fragmentDirectory, extensionOptions, result);

        if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs) {
            // If looking for a global package name, don't just include everything in `node_modules` because that includes dependencies' own dependencies.
            // (But do if we didn't find anything, e.g. 'package.json' missing.)
            let foundGlobal = false;
            if (fragmentDirectory === undefined) {
                for (const moduleName of enumerateNodeModulesVisibleToScript(host, scriptPath)) {
                    if (!result.some(entry => entry.name === moduleName)) {
                        foundGlobal = true;
                        result.push(nameAndKind(moduleName, ScriptElementKind.externalModuleName, /*extension*/ undefined));
                    }
                }
            }
            if (!foundGlobal) {
                forEachAncestorDirectory(scriptPath, ancestor => {
                    const nodeModules = combinePaths(ancestor, "node_modules");
                    if (tryDirectoryExists(host, nodeModules)) {
                        getCompletionEntriesForDirectoryFragment(fragment, nodeModules, extensionOptions, host, /*exclude*/ undefined, result);
                    }
                });
            }
        }

        return result;
    }

    function getFragmentDirectory(fragment: string): string | undefined {
        return containsSlash(fragment) ? hasTrailingDirectorySeparator(fragment) ? fragment : getDirectoryPath(fragment) : undefined;
    }

    function getCompletionsForPathMapping(
        path: string, patterns: readonly string[], fragment: string, baseUrl: string, fileExtensions: readonly string[], host: LanguageServiceHost,
    ): readonly NameAndKind[] {
        if (!endsWith(path, "*")) {
            // For a path mapping "foo": ["/x/y/z.ts"], add "foo" itself as a completion.
            return !stringContains(path, "*") ? justPathMappingName(path) : emptyArray;
        }

        const pathPrefix = path.slice(0, path.length - 1);
        const remainingFragment = tryRemovePrefix(fragment, pathPrefix);
        return remainingFragment === undefined ? justPathMappingName(pathPrefix) : flatMap(patterns, pattern =>
            getModulesForPathsPattern(remainingFragment, baseUrl, pattern, fileExtensions, host));

        function justPathMappingName(name: string): readonly NameAndKind[] {
            return startsWith(name, fragment) ? [directoryResult(name)] : emptyArray;
        }
    }

    function getModulesForPathsPattern(fragment: string, baseUrl: string, pattern: string, fileExtensions: readonly string[], host: LanguageServiceHost): readonly NameAndKind[] | undefined {
        if (!host.readDirectory) {
            return undefined;
        }

        const parsed = hasZeroOrOneAsteriskCharacter(pattern) ? tryParsePattern(pattern) : undefined;
        if (!parsed) {
            return undefined;
        }

        // The prefix has two effective parts: the directory path and the base component after the filepath that is not a
        // full directory component. For example: directory/path/of/prefix/base*
        const normalizedPrefix = resolvePath(parsed.prefix);
        const normalizedPrefixDirectory = hasTrailingDirectorySeparator(parsed.prefix) ? normalizedPrefix : getDirectoryPath(normalizedPrefix);
        const normalizedPrefixBase = hasTrailingDirectorySeparator(parsed.prefix) ? "" : getBaseFileName(normalizedPrefix);

        const fragmentHasPath = containsSlash(fragment);
        const fragmentDirectory = fragmentHasPath ? hasTrailingDirectorySeparator(fragment) ? fragment : getDirectoryPath(fragment) : undefined;

        // Try and expand the prefix to include any path from the fragment so that we can limit the readDirectory call
        const expandedPrefixDirectory = fragmentHasPath ? combinePaths(normalizedPrefixDirectory, normalizedPrefixBase + fragmentDirectory) : normalizedPrefixDirectory;

        const normalizedSuffix = normalizePath(parsed.suffix);
        // Need to normalize after combining: If we combinePaths("a", "../b"), we want "b" and not "a/../b".
        const baseDirectory = normalizePath(combinePaths(baseUrl, expandedPrefixDirectory));
        const completePrefix = fragmentHasPath ? baseDirectory : ensureTrailingDirectorySeparator(baseDirectory) + normalizedPrefixBase;

        // If we have a suffix, then we need to read the directory all the way down. We could create a glob
        // that encodes the suffix, but we would have to escape the character "?" which readDirectory
        // doesn't support. For now, this is safer but slower
        const includeGlob = normalizedSuffix ? "**/*" : "./*";

        const matches = mapDefined(tryReadDirectory(host, baseDirectory, fileExtensions, /*exclude*/ undefined, [includeGlob]), match => {
            const extension = tryGetExtensionFromPath(match);
            const name = trimPrefixAndSuffix(match);
            return name === undefined ? undefined : nameAndKind(removeFileExtension(name), ScriptElementKind.scriptElement, extension);
        });
        const directories = mapDefined(tryGetDirectories(host, baseDirectory).map(d => combinePaths(baseDirectory, d)), dir => {
            const name = trimPrefixAndSuffix(dir);
            return name === undefined ? undefined : directoryResult(name);
        });
        return [...matches, ...directories];

        function trimPrefixAndSuffix(path: string): string | undefined {
            const inner = withoutStartAndEnd(normalizePath(path), completePrefix, normalizedSuffix);
            return inner === undefined ? undefined : removeLeadingDirectorySeparator(inner);
        }
    }

    function withoutStartAndEnd(s: string, start: string, end: string): string | undefined {
        return startsWith(s, start) && endsWith(s, end) ? s.slice(start.length, s.length - end.length) : undefined;
    }

    function removeLeadingDirectorySeparator(path: string): string {
        return path[0] === directorySeparator ? path.slice(1) : path;
    }

    function getAmbientModuleCompletions(fragment: string, fragmentDirectory: string | undefined, checker: TypeChecker): readonly string[] {
        // Get modules that the type checker picked up
        const ambientModules = checker.getAmbientModules().map(sym => stripQuotes(sym.name));
        const nonRelativeModuleNames = ambientModules.filter(moduleName => startsWith(moduleName, fragment));

        // Nested modules of the form "module-name/sub" need to be adjusted to only return the string
        // after the last '/' that appears in the fragment because that's where the replacement span
        // starts
        if (fragmentDirectory !== undefined) {
            const moduleNameWithSeparator = ensureTrailingDirectorySeparator(fragmentDirectory);
            return nonRelativeModuleNames.map(nonRelativeModuleName => removePrefix(nonRelativeModuleName, moduleNameWithSeparator));
        }
        return nonRelativeModuleNames;
    }

    function getTripleSlashReferenceCompletion(sourceFile: SourceFile, position: number, compilerOptions: CompilerOptions, host: LanguageServiceHost): readonly PathCompletion[] | undefined {
        const token = getTokenAtPosition(sourceFile, position);
        const commentRanges = getLeadingCommentRanges(sourceFile.text, token.pos);
        const range = commentRanges && find(commentRanges, commentRange => position >= commentRange.pos && position <= commentRange.end);
        if (!range) {
            return undefined;
        }
        const text = sourceFile.text.slice(range.pos, position);
        const match = tripleSlashDirectiveFragmentRegex.exec(text);
        if (!match) {
            return undefined;
        }

        const [, prefix, kind, toComplete] = match;
        const scriptPath = getDirectoryPath(sourceFile.path);
        const names = kind === "path" ? getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, getExtensionOptions(compilerOptions, /*includeExtensions*/ true), host, sourceFile.path)
            : kind === "types" ? getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, getFragmentDirectory(toComplete), getExtensionOptions(compilerOptions))
            : Debug.fail();
        return addReplacementSpans(toComplete, range.pos + prefix.length, names);
    }

    function getCompletionEntriesFromTypings(host: LanguageServiceHost, options: CompilerOptions, scriptPath: string, fragmentDirectory: string | undefined, extensionOptions: ExtensionOptions, result: NameAndKind[] = []): readonly NameAndKind[] {
        // Check for typings specified in compiler options
        const seen = createMap<true>();

        const typeRoots = tryAndIgnoreErrors(() => getEffectiveTypeRoots(options, host)) || emptyArray;

        for (const root of typeRoots) {
            getCompletionEntriesFromDirectories(root);
        }

        // Also get all @types typings installed in visible node_modules directories
        for (const packageJson of findPackageJsons(scriptPath, host)) {
            const typesDir = combinePaths(getDirectoryPath(packageJson), "node_modules/@types");
            getCompletionEntriesFromDirectories(typesDir);
        }

        return result;

        function getCompletionEntriesFromDirectories(directory: string): void {
            if (!tryDirectoryExists(host, directory)) return;

            for (const typeDirectoryName of tryGetDirectories(host, directory)) {
                const packageName = unmangleScopedPackageName(typeDirectoryName);
                if (options.types && !contains(options.types, packageName)) continue;

                if (fragmentDirectory === undefined) {
                    if (!seen.has(packageName)) {
                        result.push(nameAndKind(packageName, ScriptElementKind.externalModuleName, /*extension*/ undefined));
                        seen.set(packageName, true);
                    }
                }
                else {
                    const baseDirectory = combinePaths(directory, typeDirectoryName);
                    const remainingFragment = tryRemoveDirectoryPrefix(fragmentDirectory, packageName, hostGetCanonicalFileName(host));
                    if (remainingFragment !== undefined) {
                        getCompletionEntriesForDirectoryFragment(remainingFragment, baseDirectory, extensionOptions, host, /*exclude*/ undefined, result);
                    }
                }
            }
        }
    }

    function enumerateNodeModulesVisibleToScript(host: LanguageServiceHost, scriptPath: string): readonly string[] {
        if (!host.readFile || !host.fileExists) return emptyArray;

        const result: string[] = [];
        for (const packageJson of findPackageJsons(scriptPath, host)) {
            const contents = readJson(packageJson, host as { readFile: (filename: string) => string | undefined }); // Cast to assert that readFile is defined
            // Provide completions for all non @types dependencies
            for (const key of nodeModulesDependencyKeys) {
                const dependencies: object | undefined = (contents as any)[key];
                if (!dependencies) continue;
                for (const dep in dependencies) {
                    if (dependencies.hasOwnProperty(dep) && !startsWith(dep, "@types/")) {
                        result.push(dep);
                    }
                }
            }
        }
        return result;
    }

    // Replace everything after the last directory separator that appears
    function getDirectoryFragmentTextSpan(text: string, textStart: number): TextSpan | undefined {
        const index = Math.max(text.lastIndexOf(directorySeparator), text.lastIndexOf("\\"));
        const offset = index !== -1 ? index + 1 : 0;
        // If the range is an identifier, span is unnecessary.
        const length = text.length - offset;
        return length === 0 || isIdentifierText(text.substr(offset, length), ScriptTarget.ESNext) ? undefined : createTextSpan(textStart + offset, length);
    }

    // Returns true if the path is explicitly relative to the script (i.e. relative to . or ..)
    function isPathRelativeToScript(path: string) {
        if (path && path.length >= 2 && path.charCodeAt(0) === CharacterCodes.dot) {
            const slashIndex = path.length >= 3 && path.charCodeAt(1) === CharacterCodes.dot ? 2 : 1;
            const slashCharCode = path.charCodeAt(slashIndex);
            return slashCharCode === CharacterCodes.slash || slashCharCode === CharacterCodes.backslash;
        }
        return false;
    }

    /**
     * Matches a triple slash reference directive with an incomplete string literal for its path. Used
     * to determine if the caret is currently within the string literal and capture the literal fragment
     * for completions.
     * For example, this matches
     *
     * /// <reference path="fragment
     *
     * but not
     *
     * /// <reference path="fragment"
     */
    const tripleSlashDirectiveFragmentRegex = /^(\/\/\/\s*<reference\s+(path|types)\s*=\s*(?:'|"))([^\3"]*)$/;

    const nodeModulesDependencyKeys: readonly string[] = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];

    function containsSlash(fragment: string) {
        return stringContains(fragment, directorySeparator);
    }
}


text:
node_acl_knex
=============

A total rewrite of https://github.com/christophertrudel/node_acl_knex, designed to be
a bit simpler internally, with the data organization pushed into JSON stored in a single
table.

redpajama_set_name: RedPajamaGithub


text:
package options

import (
	"io"
	"net"

	"github.com/tkuchiki/alp/helpers"
	"gopkg.in/yaml.v2"
)

const (
	DefaultSortOption      = "count"
	DefaultFormatOption    = "table"
	DefaultLimitOption     = 5000
	DefaultLocationOption  = "Local"
	DefaultOutputOption    = "all"
	DefaultPagenationLimit = 100
	// ltsv
	DefaultApptimeLabelOption = "apptime"
	DefaultReqtimeLabelOption = "reqtime"
	DefaultStatusLabelOption  = "status"
	DefaultSizeLabelOption    = "size"
	DefaultMethodLabelOption  = "method"
	DefaultUriLabelOption     = "uri"
	DefaultTimeLabelOption    = "time"
	// json
	DefaultUriKeyOption          = "uri"
	DefaultMethodKeyOption       = "method"
	DefaultTimeKeyOption         = "time"
	DefaultResponseTimeKeyOption = "response_time"
	DefaultRequestTimeKeyOption  = "request_time"
	DefaultBodyBytesKeyOption    = "body_bytes"
	DefaultStatusKeyOption       = "status"
	// regexp
	DefaultPatternOption = `^(\S+)\s` + // remote host
		`\S+\s+` +
		`(\S+\s+)+` + // user
		`\[(?P<time>[^]]+)\]\s` + // time
		`"(?P<method>\S*)\s?` + // method
		`(?P<uri>(?:[^"]*(?:\\")?)*)\s` + // URL
		`([^"]*)"\s` + // protocol
		`(?P<status>\S+)\s` + // status code
		`(?P<body_bytes>\S+)\s` + // bytes
		`"((?:[^"]*(?:\\")?)*)"\s` + // referer
		`"(?:.+)"` + // user agent
		`\s(?P<response_time>\S+)(?:\s(?P<request_time>\S+))?$`
	DefaultUriSubexpOption          = "uri"
	DefaultMethodSubexpOption       = "method"
	DefaultTimeSubexpOption         = "time"
	DefaultResponseTimeSubexpOption = "response_time"
	DefaultRequestTimeSubexpOption  = "request_time"
	DefaultBodyBytesSubexpOption    = "body_bytes"
	DefaultStatusSubexpOption       = "status"
	// pcap
	DefaultPcapServerPortOption = 80
)

var DefaultPercentilesOption = []int{90, 95, 99}

var DefaultPcapServerIPsOption = getDefaultPcapServerIPsOption()

type Options struct {
	File                    string         `yaml:"file"`
	Sort                    string         `yaml:"sort"`
	Reverse                 bool           `yaml:"reverse"`
	QueryString             bool           `yaml:"query_string"`
	QueryStringIgnoreValues bool           `yaml:"query_string_ignore_values"`
	DecodeUri               bool           `yaml:"decode_uri"`
	Format                  string         `yaml:"format"`
	NoHeaders               bool           `yaml:"noheaders"`
	ShowFooters             bool           `yaml:"show_footers"`
	Limit                   int            `yaml:"limit"`
	MatchingGroups          []string       `yaml:"matching_groups"`
	Filters                 string         `yaml:"filters"`
	PosFile                 string         `yaml:"pos_file"`
	NoSavePos               bool           `yaml:"nosave_pos"`
	Location                string         `yaml:"location"`
	Output                  string         `yaml:"output"`
	Percentiles             []int          `yaml:"percentiles"`
	PaginationLimit         int            `yaml:"pagination_limit"`
	LTSV                    *LTSVOptions   `yaml:"ltsv"`
	Regexp                  *RegexpOptions `yaml:"regexp"`
	JSON                    *JSONOptions   `yaml:"json"`
	Pcap                    *PcapOptions   `yaml:"pcap"`
}

type LTSVOptions struct {
	ApptimeLabel string `yaml:"apptime_label"`
	ReqtimeLabel string `yaml:"reqtime_label"`
	StatusLabel  string `yaml:"status_label"`
	SizeLabel    string `yaml:"size_label"`
	MethodLabel  string `yaml:"method_label"`
	UriLabel     string `yaml:"uri_label"`
	TimeLabel    string `yaml:"time_label"`
}

type RegexpOptions struct {
	Pattern            string `yaml:"pattern"`
	UriSubexp          string `yaml:"uri_subexp"`
	MethodSubexp       string `yaml:"method_subexp"`
	TimeSubexp         string `yaml:"time_subexp"`
	ResponseTimeSubexp string `yaml:"response_time_subexp"`
	RequestTimeSubexp  string `yaml:"request_time_subexp"`
	BodyBytesSubexp    string `yaml:"body_bytes_subexp"`
	StatusSubexp       string `yaml:"status_subexp"`
}

type JSONOptions struct {
	UriKey          string `yaml:"uri_key"`
	MethodKey       string `yaml:"method_key"`
	TimeKey         string `yaml:"time_key"`
	ResponseTimeKey string `yaml:"response_time_key"`
	RequestTimeKey  string `yaml:"request_time_key"`
	BodyBytesKey    string `yaml:"body_bytes_key"`
	StatusKey       string `yaml:"status_key"`
}

type PcapOptions struct {
	ServerIPs  []string `yaml:"server_ips"`
	ServerPort uint16   `yaml:"server_port"`
}

type Option func(*Options)

func File(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.File = s
		}
	}
}

func Sort(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Sort = s
		}
	}
}

func Reverse(b bool) Option {
	return func(opts *Options) {
		if b {
			opts.Reverse = b
		}
	}
}

func QueryString(b bool) Option {
	return func(opts *Options) {
		if b {
			opts.QueryString = b
		}
	}
}

func QueryStringIgnoreValues(b bool) Option {
	return func(opts *Options) {
		if b {
			opts.QueryStringIgnoreValues = b
		}
	}
}

func DecodeUri(b bool) Option {
	return func(opts *Options) {
		if b {
			opts.DecodeUri = b
		}
	}
}

func Format(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Format = s
		}
	}
}

func NoHeaders(b bool) Option {
	return func(opts *Options) {
		if b {
			opts.NoHeaders = b
		}
	}
}

func ShowFooters(b bool) Option {
	return func(opts *Options) {
		if b {
			opts.ShowFooters = b
		}
	}
}

func Limit(i int) Option {
	return func(opts *Options) {
		if i > 0 {
			opts.Limit = i
		}
	}
}

func MatchingGroups(values []string) Option {
	return func(opts *Options) {
		if len(values) > 0 {
			opts.MatchingGroups = values
		}
	}
}

func CSVGroups(csv string) Option {
	return func(opts *Options) {
		a := helpers.SplitCSV(csv)
		if len(a) > 0 {
			opts.MatchingGroups = a
		}
	}
}

func Filters(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Filters = s
		}
	}
}

func Location(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Location = s
		}
	}
}

func Output(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Output = s
		}
	}
}

func PosFile(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.PosFile = s
		}
	}
}

func NoSavePos(b bool) Option {
	return func(opts *Options) {
		if b {
			opts.NoSavePos = b
		}
	}
}

func Percentiles(i []int) Option {
	return func(opts *Options) {
		if len(i) > 0 {
			opts.Percentiles = i
		}
	}
}

func PaginationLimit(i int) Option {
	return func(opts *Options) {
		if i > 0 {
			opts.PaginationLimit = i
		}
	}
}

// ltsv
func ApptimeLabel(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.LTSV.ApptimeLabel = s
		}
	}
}

func ReqtimeLabel(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.LTSV.ReqtimeLabel = s
		}
	}
}

func StatusLabel(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.LTSV.StatusLabel = s
		}
	}
}

func SizeLabel(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.LTSV.SizeLabel = s
		}
	}
}

func MethodLabel(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.LTSV.MethodLabel = s
		}
	}
}

func UriLabel(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.LTSV.UriLabel = s
		}
	}
}

func TimeLabel(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.LTSV.TimeLabel = s
		}
	}
}

// regexp
func Pattern(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Regexp.Pattern = s
		}
	}
}

func UriSubexp(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Regexp.UriSubexp = s
		}
	}
}

func MethodSubexp(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Regexp.MethodSubexp = s
		}
	}
}

func TimeSubexp(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Regexp.TimeSubexp = s
		}
	}
}

func ResponseTimeSubexp(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Regexp.ResponseTimeSubexp = s
		}
	}
}

func RequestTimeSubexp(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Regexp.RequestTimeSubexp = s
		}
	}
}

func BodyBytesSubexp(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Regexp.BodyBytesSubexp = s
		}
	}
}

func StatusSubexp(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.Regexp.StatusSubexp = s
		}
	}
}

// json
func UriKey(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.JSON.UriKey = s
		}
	}
}

func MethodKey(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.JSON.MethodKey = s
		}
	}
}

func TimeKey(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.JSON.TimeKey = s
		}
	}
}

func ResponseTimeKey(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.JSON.ResponseTimeKey = s
		}
	}
}

func RequestTimeKey(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.JSON.RequestTimeKey = s
		}
	}
}

func BodyBytesKey(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.JSON.BodyBytesKey = s
		}
	}
}

func StatusKey(s string) Option {
	return func(opts *Options) {
		if s != "" {
			opts.JSON.StatusKey = s
		}
	}
}

// pcap
func PcapServerIPs(ss []string) Option {
	return func(opts *Options) {
		if len(ss) > 0 {
			opts.Pcap.ServerIPs = ss
		}
	}
}

func PcapServerPort(n uint16) Option {
	return func(opts *Options) {
		if n != 0 {
			opts.Pcap.ServerPort = n
		}
	}
}

func NewOptions(opt ...Option) *Options {
	ltsv := &LTSVOptions{
		ApptimeLabel: DefaultApptimeLabelOption,
		ReqtimeLabel: DefaultReqtimeLabelOption,
		StatusLabel:  DefaultStatusLabelOption,
		SizeLabel:    DefaultSizeLabelOption,
		MethodLabel:  DefaultMethodLabelOption,
		UriLabel:     DefaultUriLabelOption,
		TimeLabel:    DefaultTimeLabelOption,
	}

	regexp := &RegexpOptions{
		Pattern:            DefaultPatternOption,
		UriSubexp:          DefaultUriSubexpOption,
		MethodSubexp:       DefaultMethodSubexpOption,
		TimeSubexp:         DefaultTimeSubexpOption,
		ResponseTimeSubexp: DefaultResponseTimeSubexpOption,
		RequestTimeSubexp:  DefaultRequestTimeSubexpOption,
		BodyBytesSubexp:    DefaultBodyBytesSubexpOption,
		StatusSubexp:       DefaultStatusSubexpOption,
	}

	json := &JSONOptions{
		UriKey:          DefaultUriKeyOption,
		MethodKey:       DefaultMethodKeyOption,
		TimeKey:         DefaultTimeKeyOption,
		ResponseTimeKey: DefaultResponseTimeKeyOption,
		RequestTimeKey:  DefaultRequestTimeKeyOption,
		BodyBytesKey:    DefaultBodyBytesKeyOption,
		StatusKey:       DefaultStatusKeyOption,
	}

	pcap := &PcapOptions{
		ServerIPs:  DefaultPcapServerIPsOption,
		ServerPort: DefaultPcapServerPortOption,
	}

	options := &Options{
		Sort:            DefaultSortOption,
		Format:          DefaultFormatOption,
		Limit:           DefaultLimitOption,
		Location:        DefaultLocationOption,
		Output:          DefaultOutputOption,
		Percentiles:     DefaultPercentilesOption,
		PaginationLimit: DefaultPagenationLimit,
		LTSV:            ltsv,
		Regexp:          regexp,
		JSON:            json,
		Pcap:            pcap,
	}

	for _, o := range opt {
		o(options)
	}

	return options
}

func SetOptions(options *Options, opt ...Option) *Options {
	for _, o := range opt {
		o(options)
	}

	return options
}

func LoadOptionsFromReader(r io.Reader) (*Options, error) {
	opts := NewOptions()
	buf, err := io.ReadAll(r)
	if err != nil {
		return opts, err
	}

	configs := NewOptions()
	err = yaml.Unmarshal(buf, configs)

	opts = SetOptions(opts,
		Sort(configs.Sort),
		Limit(configs.Limit),
		Location(configs.Location),
		Output(configs.Output),
		Reverse(configs.Reverse),
		File(configs.File),
		QueryString(configs.QueryString),
		QueryStringIgnoreValues(configs.QueryStringIgnoreValues),
		DecodeUri(configs.DecodeUri),
		Format(configs.Format),
		NoHeaders(configs.NoHeaders),
		ShowFooters(configs.ShowFooters),
		PosFile(configs.PosFile),
		NoSavePos(configs.NoSavePos),
		MatchingGroups(configs.MatchingGroups),
		Filters(configs.Filters),
		Percentiles(configs.Percentiles),
		PaginationLimit(configs.PaginationLimit),
		// ltsv
		ApptimeLabel(configs.LTSV.ApptimeLabel),
		ReqtimeLabel(configs.LTSV.ReqtimeLabel),
		StatusLabel(configs.LTSV.StatusLabel),
		SizeLabel(configs.LTSV.SizeLabel),
		MethodLabel(configs.LTSV.MethodLabel),
		UriLabel(configs.LTSV.UriLabel),
		TimeLabel(configs.LTSV.TimeLabel),
		// json
		ResponseTimeKey(configs.JSON.ResponseTimeKey),
		RequestTimeKey(configs.JSON.RequestTimeKey),
		StatusKey(configs.JSON.StatusKey),
		BodyBytesKey(configs.JSON.BodyBytesKey),
		MethodKey(configs.JSON.MethodKey),
		UriKey(configs.JSON.UriKey),
		TimeKey(configs.JSON.TimeKey),
		// regexp
		Pattern(configs.Regexp.Pattern),
		ResponseTimeSubexp(configs.Regexp.ResponseTimeSubexp),
		RequestTimeSubexp(configs.Regexp.RequestTimeSubexp),
		StatusSubexp(configs.Regexp.StatusSubexp),
		BodyBytesSubexp(configs.Regexp.BodyBytesSubexp),
		MethodSubexp(configs.Regexp.MethodSubexp),
		UriSubexp(configs.Regexp.UriSubexp),
		TimeSubexp(configs.Regexp.TimeSubexp),
		// pcap
		PcapServerIPs(configs.Pcap.ServerIPs),
		PcapServerPort(configs.Pcap.ServerPort),
	)

	return opts, err
}

func getDefaultPcapServerIPsOption() (ips []string) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		// fallback
		return []string{"127.0.0.1", "::1"}
	}

	for _, addr := range addrs {
		ipNet, isIP := addr.(*net.IPNet)
		if !isIP {
			continue
		}

		ips = append(ips, ipNet.IP.String())
	}
	if len(ips) == 0 {
		// fallback
		return []string{"127.0.0.1", "::1"}
	}

	return
}


text:
package org.lealone.cgroup.subsystem;

import org.lealone.cgroup.Group;

public class Cpu extends SubSystem {

    private static final String CPU_STAT = "cpu.stat";
    private static final String CPU_SHARES = "cpu.shares";

    private static final String CPU_CFS_PERIOD_US = "cpu.cfs_period_us";
    private static final String CPU_CFS_QUOTA_US = "cpu.cfs_quota_us";
    private static final String CPU_RT_PERIOD_US = "cpu.rt_period_us";
    private static final String CPU_RT_RUNTIME_US = "cpu.rt_runtime_us";

    public Cpu(Group group) {
        super(group);
    }

    @Override
    public SubSystemType getType() {
        return SubSystemType.cpu;
    }

    public static class Stat {
        public final int nrPeriods;
        public final int nrThrottled;
        public final int throttledTime;

        public Stat(String statStr) {
            String[] splits = statStr.split("\n");
            this.nrPeriods = Integer.parseInt(splits[0].split(" ")[1]);
            this.nrThrottled = Integer.parseInt(splits[1].split(" ")[1]);
            this.throttledTime = Integer.parseInt(splits[2].split(" ")[1]);
        }
    }

    public Stat getStat() {
        String output = getStringParameter(CPU_STAT);
        Stat stat = new Stat(output);
        return stat;
    }

    public void setShares(int shares) {
        setParameter(CPU_SHARES, shares);
    }

    public int getShares() {
        return getIntParameter(CPU_SHARES);
    }

    public void setCfsPeriodUs(long v) {
        setParameter(CPU_CFS_PERIOD_US, v);
    }

    public long getCfsPeriodUs() {
        return getLongParameter(CPU_CFS_PERIOD_US);
    }

    public void setCfsQuotaUs(long v) {
        setParameter(CPU_CFS_QUOTA_US, v);
    }

    public long getCfsQuotaUs() {
        return getLongParameter(CPU_CFS_QUOTA_US);
    }

    public void setRtPeriodUs(long v) {
        setParameter(CPU_RT_PERIOD_US, v);
    }

    public long getRtPeriodUs() {
        return getLongParameter(CPU_RT_PERIOD_US);
    }

    public void setRtRuntimeUs(long v) {
        setParameter(CPU_RT_RUNTIME_US, v);
    }

    public long getRtRuntimeUs() {
        return getLongParameter(CPU_RT_RUNTIME_US);
    }

}


text:
using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using RiceDoctor.Shared;

namespace RiceDoctor.WebApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var filePath = Path.Combine(AppContext.BaseDirectory, "log.txt");
            var logger = new FileLogger(filePath);
            Logger.OnLog += logger.Log;

            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseApplicationInsights()
                .Build();

            host.Run();
        }
    }
}


text:
/*
  GP20U7 GPS Library - Basic
 Demonstrates the use of the GP20U7 GPS unit by retrieving the latitude
 and longitude of your current position.

 The circuit:
 * GP20U7 TX pin to digital pin 0/RX
 * GP20U7 VCC pin to 3.3V
 * GP20U7 GND pin to ground

 NOTE: The GPS unit can take up to a minute or more to acquire a position
 lock and start outputting data. That means you may not see the serial
 output right away which is normal.

 */

// include the library code:
#include "gp20u7_particle/gp20u7_particle.h"

// initialize the library with the serial port to which the device is
// connected
GP20U7 gps = GP20U7(Serial1);

// Set up a Geolocation variable to track your current Location
Geolocation currentLocation;

void setup() {
  // Start the GPS module
  gps.begin();

  // Setup virtual comm port for output
  Serial.begin(9600);
}

void loop() {
  // The call to read() checks for new location information and returns 1
  // if new data is available and 0 if it isn't. It should be called
  // repeatedly.
  if(gps.read()){
    currentLocation = gps.getGeolocation();
    Serial.println("Your current location is:");
    Serial.print("    Latitude: ");
    Serial.println(currentLocation.latitude,5);
    Serial.print("    Longitude: ");
    Serial.println(currentLocation.longitude,5);
  }
}


text:
package router

import (
	"encoding/json"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
	"github.com/pivotal-golang/lager"
	"github.com/pivotal-golang/lager/lagertest"

	"testing"

	"github.com/cloudfoundry-incubator/cf-tcp-router-acceptance-tests/helpers"
	"github.com/cloudfoundry-incubator/cf-tcp-router/testutil"
)

func TestRouter(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Router Suite")
}

var (
	sampleReceiverPath string
	externalIP         string
	routerApiConfig    helpers.RouterApiConfig
	logger             lager.Logger
)

var _ = SynchronizedBeforeSuite(func() []byte {
	sampleReceiver, err := gexec.Build("github.com/cloudfoundry-incubator/cf-tcp-router-acceptance-tests/assets/tcp-sample-receiver", "-race")
	Expect(err).NotTo(HaveOccurred())
	payload, err := json.Marshal(map[string]string{
		"sample-receiver": sampleReceiver,
	})

	Expect(err).NotTo(HaveOccurred())

	return payload
}, func(payload []byte) {
	context := map[string]string{}

	err := json.Unmarshal(payload, &context)
	Expect(err).NotTo(HaveOccurred())

	sampleReceiverPath = context["sample-receiver"]
	externalIP = testutil.GetExternalIP()
	routerApiConfig = helpers.LoadConfig()
	logger = lagertest.NewTestLogger("test")
})

var _ = SynchronizedAfterSuite(func() {
}, func() {
	gexec.CleanupBuildArtifacts()
})


text:
require 'ansible/ruby/modules/base'

module Ansible
  module Ruby
    module Modules
      # Bind interface selector profiles to switch policy leaf profiles on Cisco ACI fabrics.
      class Aci_interface_selector_to_switch_policy_leaf_profile < Base
        # @return [String, nil] Name of the Leaf Profile to which we add a Selector.
        attribute :leaf_profile
        validates :leaf_profile, type: String

        # @return [String, nil] Name of Interface Profile Selector to be added and associated with the Leaf Profile.
        attribute :interface_selector
        validates :interface_selector, type: String

        # @return [:absent, :present, :query, nil] Use C(present) or C(absent) for adding or removing.,Use C(query) for listing an object or multiple objects.
        attribute :state
        validates :state, expression_inclusion: {:in=>[:absent, :present, :query], :message=>"%{value} needs to be :absent, :present, :query"}, allow_nil: true
      end
    end
  end
end


text:
<?php

/**
 * MaToDo - easy TODO
 *
 * @license    MIT
 * @link       https://github.com/bakerman/matodo
 * @author     Milan Pekar
 */

namespace App\Presenters;

use Nette;

/**
 * Base presenter for all application presenters.
 */
abstract class BasePresenter extends Nette\Application\UI\Presenter
{

	public function startup()
	{
		parent::startup();

		$this->template->appName = $this->context->parameters['appName'];

		// check authentication
		if (!$this->getUser()->isLoggedIn() && !in_array($this->name, ['Error', 'Sign'])) {
			$this->flashMessage('Přihlašte se, prosím');
			$this->redirect('Sign:');
		}
	}

}


text:
require_relative "benzinator/version"
require_relative "benzinator/car"

module Benzinator
end