<commit_before>// Copyright (c) 2013, <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package util

import (
	"errors"
	"fmt"
	"os"
)

// IsExists returns exists = true if the given file exists, regardless whether
// it is a file or a directory. Normally you will probably want to use the more
// specific versions: IsFileExists and IsDirectoryExists.
func IsExists(path string) (exists bool, err error) {
	exists, _, err = isExists(path)
	return
}

// IsFileExists returns exists = true if the file exists and is not
// a directory, and returns err != nil if any other error occured (such as
// permission denied).
func IsFileExists(path string) (exists bool, err error) {
	var stat os.FileInfo

	exists, stat, err = isExists(path)
	if err != nil && stat.IsDir() {
		err = errors.New(fmt.Sprintf("%s exists but is a directory not a file", path))
	}
	return
}

// IsDirectoryExists returns exists = true if the file exists and is
// a directory, and returns err != nil if any other error occured (such as
// permission denied).
func IsDirectoryExists(path string) (exists bool, err error) {
	var stat os.FileInfo

	exists, stat, err = isExists(path)
	if err != nil && !stat.IsDir() {
		err = errors.New(fmt.Sprintf("%s exists but is not a directory", path))
	}
	return
}

// isExists does the hard work for IsExist, IsFileExist, and IsDirectoryExists,
// returning exists = true if the file given by path exists.
func isExists(path string) (exists bool, stat os.FileInfo, err error) {
	stat, err = os.Stat(path)

	exists = true
	if err != nil {
		// exists = true if file exists
		exists = !os.IsNotExist(err)
		if !exists {
			err = nil
		}
	}
	return
}<commit_msg>Improving function names and simplifying code<commit_after>// Copyright (c) 2013, <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package util

import (
	"fmt"
	"os"
)

// Exists returns exists = true if the given file exists, regardless whether
// it is a file or a directory. Normally you will probably want to use the more
// specific versions: FileExists and DirectoryExists.
func Exists(path string) (ex bool, err error) {
	ex, _, err = exists(path)
	return ex, err
}

// FileExists returns exists = true if the file exists and is not
// a directory, and returns err != nil if any other error occured (such as
// permission denied).
func FileExists(path string) (ex bool, err error) {
	var stat os.FileInfo

	ex, stat, err = exists(path)
	if err != nil && stat.IsDir() {
		err = fmt.Errorf("%s exists but is a directory not a file", path)
	}
	return ex, err
}

// DirectoryExists returns exists = true if the file exists and is
// a directory, and returns err != nil if any other error occured (such as
// permission denied).
func DirectoryExists(path string) (ex bool, err error) {
	var stat os.FileInfo

	ex, stat, err = exists(path)
	if err != nil && !stat.IsDir() {
		err = fmt.Errorf("%s exists but is not a directory", path)
	}
	return ex, err
}

// exists does the hard work for Exists, FileExists, and DirectoryExists,
// returning ex = true if the file given by path exists.
func exists(path string) (ex bool, stat os.FileInfo, err error) {
	stat, err = os.Stat(path)

	ex = true
	if err != nil {
		// ex = true if file exists
		ex = !os.IsNotExist(err)
		if !ex {
			err = nil
		}
	}
	return ex, stat, err
}
{'fraction_non_alphanumeric': 0.10433526011560694, 'fraction_numerical': 0.0023121387283236996, 'mean_word_length': 3.477360931435964, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3116769', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1052, 'n_words': 481}